My opinion about Template in PHP sway all the time. Right now, I am on the "they are good for x reasons" side of the fence.
However, I don't know of any templating engines that do templating right.
Unfortunately, this is the case with this template engine here as well. Now, just quickly, I should point out that I have developed a template engine as well, because, well, all the ones I have seen (including Smarty) fail the "Visual Designer" test. If I can't pull a template into a visual designer and accurately see the result, the template fails. The designer might as well use XSL.
This is the case here as well.
Take the sample provide:
Code: Select all
<їBLOCK emailtable]>
<table>
<tr><td>Name</td><td>email</td></tr>
<їBLOCKLOOP emailtable]>
<tr><td><їVAR name]></td><td><їVAR email]></td></tr>
<ї/BLOCKLOOP emailtable]>
</table>
<ї/BLOCK emailtable]>
A visual editor (such as dreamweaver) could not accurately display this template.
First, I don't know if the visual editor would munge with the funky tags.
However, a bigger problem (and the same one Smarty suffers from, is the variable printing problem.
tells me that it will print a var named name. But this is not good, because when I put this into a template, and then try to view the template, I won't see a thing. So I can visually style my document. {header} would be better here for variables that would be displayed. That way, I can see, visually, the style as I am creating the template.
Simply, XML based templates, or those that try to act like them, are bad for designers. Designers don't design using XML, they design visually. Programming create these templating engines with the thought of what THEY think designers need.
Compare the above with a sample template from my templating engine below.
Code: Select all
<!-- INCLUDE header -->
<table>
<caption>
<!-- IF {userIsLoggedIn} -->
<b>User logged in.</b>
<!-- ELSE IF {userStatus} eq 'active' -->
<b>User status is active</b>
<!-- ELSE -->
User not logged in.
<!-- END IF -->
</caption>
<!-- LOOP ON people FOR EACH family -->
<!-- {debug} -->
<tr>
<!-- LOOP ON family FOR EACH name -->
<td>
{ifEmpty; name.first,No First Name}
{!makeBold;name.last}
</td>
<!-- END LOOP -->
</tr>
<!-- END LOOP -->
</table>
<!-- INCLUDE footer -->
The example above works well for a visual designer. They can create this template using common things. For example, if they used Dreamweaver MX, they could simple create a simple template, and from design view, type in the tag they want. The comments are all block level elements.
The power in the class is also the ability to modify these, so if you didn't want to use LOOP ON, you could instead use LOOPON, or just LOOP, however your designers wanted.
The ability to Cache is handled seperatly (as it should be), but easily done.
Functions and extensions are easy to create. The difference between the two is subtle, yet important. An extension actually builds what is displayed. For example, here is the ifEmpty extension:
Code: Select all
<?php
function extension_ifEmpty ( $args )
{
list($var, $default_value) = explode(_CT_EXTENSION_VARIABLE_SEPERATOR, $args);
$var = CerealTemplateCompiler::compile_var_names($var);
$return_code = "<?php echo (empty($var) ? '$default_value' : $var); ?>";
return $return_code;
}
?>
Where as, with a function, it merely calls a function which returns a value. !makeBold is such an example. {!makeBold; name.first}, when compiled, looks like this:
Code: Select all
<?php echo makeBold($name['first']); ?>
Of course, this templating engine also allows you to do this:
If you need, you could also pass multiple arguments to the function (or extension) as well.
McGruff: The prime benefit of not using PHP as you templating engine is security. As soon as you allow PHP into a template, you allow access to a wide array of functionality. With a template, you can control that.
Even in a work environment, where everyone is working at the same company, designers and programmers, preventing access to PHP from the designers is a good thing. A designer can't come along, and include bad PHP code. One thing I am doing with my template engine is a syntax checker. It won't create the file if the syntax is wrong, which goes a step to self-checking.
Using PHP as your template engine means if a designer forgets something, or makes a syntax mistake, and uploads the template, it is ready, and run, and can cause problems. Whereas if a designer uploads a template, and the template won't compile because the syntax is wrong, it means the compiled version is still okay, and the site runs normally.
Other reasons are fairly obvious. If you look around at most of the popular programs, PHP or not, most include some sort of skinning mechanism. They all do this using their own 'templating' system.' Even phpBB runs off a templating engine.
There are people who don't think non-PHP templates are a good idea (and truth be told, I was one of them), however, they are better than PHP in many cases.
Another case is when you are hosting a service for people to use. If you want people to be able to customize the interface, PHP is NOT the way to go. Take a checkout for an online credit card processor. Each merchant needs to send their customers to the processor to get checked out. However, the merchant wants to customize the header and the footer of the site. PHP is way to insecure to let them going mucking around with PHP. A template engine again is needed.
It's not the holy grail or a sliver bullet, but it's better to know when template engines are useful, and when they aren't.