Xanax onlineAdderall onlineLevitraviagra without prescriptionadderall onlineadderall without prescriptionPhentermine onlinetramadol onlinevalium online


PHP template engines and why they’re useless

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)

5 Responses to “PHP template engines and why they’re useless”

  • Skrol29

    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.

  • Galen

    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.

  • dbforch

    Skrol29 and many many others. You don’t get the point of a template. A template is designed to separate “business” logic from the “presentation” logic. Not HTML and PHP. The templates function IS a “PRESENTATION” layer, nothing more, nothing less. It should be specified to your needs or in your own ‘logic’.

    What Galen describes is the on going discussion between Smarty developers and others (like me). Though this doesn’t mean a template engine is a bad thing. Certainly you can build some specific model that does a lot of work for you in words of classes and methods, but not the over all extra and very confusing syntaxes to work with an IF/ELSE or FOREACH loop. To proof just very simple that Smarty and a lot of others missed the point of a template engine is the default value for variables -> {$blup|”default value”}. This is not what a template is meant for, because the default value belongs definitely business layer (data exchange between the source of information; e.g database, and the interface).

    Cheers

  • Galen

    well said my friend

  • The Logic Behind Template Engines (PHP-Based) | Yum yum ..

    [...] PHP like any other programmer once started with a certain language. Just recently, I stumbled upon galengrover.com‘s blog. He posted a small statement on PHP template engines and why they are useless. Skrol29 [...]

Leave a Reply

You must be logged in to post a comment.