patrikG wrote:using a template engine and not inline-PHP for the presentation layer can, to my mind, only be justified with two reasons:
1. you have designers who you do not want to be able to write PHP code
2. you want to minimise hits on the database
Template engines will become more or less a recreation of a scripting language (e.g. PHP) - so why not use PHP? Templates are only justifiable if they reduce complexity, more often than not they increase it, which is plain silly.
3. portions of the template will change depending on what page you're on, but you still need to keep the main template file consistent across pages.
4. when an item is activated (div layer's visibility toggled, menu item added via javascript, etc..) it's state needs to be maintained between page views.
... I'm going to be adding menu items, using javascript when certain events are triggered, and once a menu item is added, an RPC call is made to alert PHP of the new menu item, which is then serialized into the template class, and stored into the session, so now every page has that menu item activated.
I have about 10 other problems besides that which the easiest solution for me was a templating class. I created this thread not to debate the use of templates, but to ask how I should implement the setting of defaults, which nielsene answered very well.
Don't get me wrong you all have legitimate points, they just do not apply to my specific project..
and to further clarify the reason I'm not just doing
Code: Select all
$user = unserialize($_SESSION['user']);
if ( $user -> validate() ) {
// output welcome msg
} else {
// output login box
}
is because when a user logs in, the welcome message appears through javascript RPC's, I cannot have the page refresh between tasks such as logging in, or clicking menu items, the site has a lot of flash content and for example my login box is in flash, when they login it will fade from the login form to the welcome message..
also simply because not all the states I'm maintaining are booleans, sometimes I may need to trasfer the contents of a div layer from page to page, while all of this can be acheived through mixed html/php documents, that's just silly to me... just like the use of a templating class is silly to you I guess..
Anyways I have my issue solved and I don't think it needs to be discussed further, I really do appreciate everyone taking the time to come up with their suggestions but really the only one that helped much was nielsene's.
----------------------------------------------------
edwardaux:
What I would do in your situation is first come up with all the data I need to build the page, then I would include a template file looking something like this:
Code: Select all
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<h3><?=title?></h3>
You have <?=count($product_array)?> items in your shopping cart:
<table>
<tr>
<td></td>
<td></td>
</tr>
<?
while(list($name,$price)=explode("-", each($product_array)) {
<tr>
<td><?=$name?></td>
<td><?=price?></td>
</tr>
}
?>
</table>
</body>
<html>
just basically embed all your data right into the template, you can keep the code that's actually getting the data seperate though. In your scenario I would have to agree with mcgruff, you shouldn't really go overboard with the templating, if you're going to have customizeable elements of the page, for example the user can set the page's background color or something like that, you're best off using CSS and just telling the browser which CSS to use for which pages.