For the past 4 or 5 years I’ve been a proponent of using PHP as it’s own template engine. I cringe every time I see someone using smarty, btemplate, fastTemplate, etc… because I’ve never had to do anything in my html that couldn’t be done with PHP. Never! Why add a layer to your app if you don’t have to?
To illustrate this I’m going to show you a template written in a template engine, and then in PHP.
Here’s an example from the Smarty website
<table border="0" width="300">
<tr>
<th colspan="2" bgcolor="#d1d1d1">Guestbook Entries (<a href="{$SCRIPT_NAME}?action=add">add</a>)</th>
</tr>
{foreach from=$data item="entry"}
<tr bgcolor="{cycle values="#dedede,#eeeeee" advance=false}">
<td>{$entry.Name|escape}</td>
<td align="right">{$entry.EntryDate|date_format:"%e %b, %Y %H:%M:%S"}</td>
</tr>
<tr>
<td colspan="2" bgcolor="{cycle values="#dedede,#eeeeee"}">{$entry.Comment|escape}</td>
</tr>
{foreachelse}
<tr>
<td colspan="2">No records</td>
</tr>
{/foreach}
</table>Here’s an example using PHP. e() is an escaping function you write yourself.
<table border="0" width="300">
<tr>
<th colspan="2" bgcolor="#d1d1d1">Guestbook Entries (<a href="{$SCRIPT_NAME}?action=add">add</a>)</th>
</tr>
<?php if ( count( $data ) ): ?>
<?php foreach( $data as $entry ): ?>
<tr bgcolor="<?php if( $i++ % 2 ): ?>#dedede<?php else: ?>#eeeeee<?php endif; ?>">
<td><?= e( $entry['name'] ) ?></td>
<td align="right"><?= date( $entry['entryDate'], 'e b Y H M S' ) ?></td>
</tr>
<tr>
<td colspan="2" bgcolor="<?php if( $i % 2 ): ?>#dedede<?php else: ?>#eeeeee<?php endif; ?>"><?= e( $entry['comment'] ) ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="2">No records</td>
</tr>
<?php endif; ?>
</table>As you can see there is only 2 added lines of code. I had to add an if statement to account for smarty’s foreachelse. No added layer. No extra work for the interpreter. Just as readable (unless you’re a developer, then it will be more readable)
In closing read this post on the sitepoint forums. Pay particular attention to Voostind’s replies (#1, #2, #3)
Well, of course I cannot agree with you assertion. (I’m a coder of the TinyButStrong Template Engine)
You say “I’ve never had to do anything in my HTML that couldn’t be done with PHP”. But the point of template engines is not to do things in the HTML, but to not do HTML in the PHP, and (as possible can be) to not do PHP in the HTML template.
Smarty doest succeed really in the second point because its tag syntax is made very closed to the PHP syntax in order to be easily turned (they say “compiled”) into PHP. Therefore, coding Smarty template is sometime very closed to coding PHP, as you have underlined it.
Nevertheless, as soon as the purpose is an human interface, the coding language naturally needs a template engine philosophy. PHP is not a template engine that enables us to have a pure PHP script and a pure HTML template that can be both designed with appropriate editors.
What lot of template engines are missing is templates that should be editable in all WYSIWYG/Visual Editors.
I should have been more careful in my explanation. I don’t put complex code in my templates. Basic looping structures, single function calls, importing of other templates.. that’s about all I do in my templates. Everything else is covered in my models/controllers, so I have no need for anything else.
I use notepad++ and my PHP templates looks just find in them for my needs. If I needed specific formatting I think my time would be better spent creating a custom-defined format than learning a new template engine.