www.tinybutstrong.com
Skrol29, 2006-06-25

Doing PHP templates of the third kind

This article gathers PHP template engine philosophies and introduces a new way of templating with PHP.

A PHP template engine is a solution which enables you to code PHP applications or documents by separating the user interface construction from the backstage management (mostly dealing with data). In technical terms, it is called separating design logic from business logic.
Lot of PHP template engines have born, many are stopped, some are famous. It seems that templating with PHP is a really need that has not been solved yet. It seems that coders are still waiting for a template engine of <magic sound with reverb> the third kind ... </magic sound with reverb>.

What are the three kinds of PHP template engine we have ?

~~~ clash of cymbals ~~~

First kind: PHP templates, the Rasmus way

The goal of this method is to predefine layout and design. Rasmus Lerdorf, (the man who created PHP in 1995) asserts that PHP itself was originally a templating system and still is. In the approach he supports, the template is a PHP script containing HTML with simple PHP function calls. It is accompanied by a template helper script containing the custom functions needed by the template.Those functions can eventually supplement inside HTML code. The business logic is done by another script which is pure PHP. It prepares data and calls the template.

Example of PHP template:
<?title('Example Page')?>
<?greeting()?>
<h1>Heading 1</h1>
  <p> Page content </p>
<h1>Heading 2</h1>
  <p> Yada Yada </p>
<h1>Heading 3</h1>
  <p> Page content </p>
<?footer()?>

~~~ clash of cymbals ~~~

Second kind: programmed templates, the Smarty way

The goal here is not only to predefine but also to simplify layout and design. The template is a text file containing the source of the visual document you want to produce, but it also includes engine's specific tags for placing dynamic contents. Smarty is the most known PHP template engine doing this way, it has 35 properties and 38 methods. Smarty's specific tags have to be preassigned to some data before to be merged. This is done by the engine at PHP side. Example: $smarty->assign('title',$title). At the end, when all specific tags of the template are preassigned to data, they can be merged together with one call: $smarty->display(). The Smarty specific tag's syntax has a common logic with the PHP syntax for conditions, iterations and formatting. Thus, it doesn't disturb coder work and enables a template to eventually be combined with its corresponding PHP assignments in order to produce a pregenerated page in pure PHP.

Example of programmed template:
<table>
 {section name=mysec loop=$users}
 {strip}
   <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
    <td>{$users[mysec].name}</td>
    <td>{$users[mysec].phone}</td>
   </tr>
 {/strip}
 {/section}
</table>

~~~ clash of cymbals ~~~

Third kind: natural templates, the TinyButStrong way

The goal is to simplify more the previous approach, by having specific tags no more coded but compatible with the nature of the template. As far as we know, TinyButStrong is the only PHP template engine which enables wysiwyg templates. Using the trick of relative tags, you can define inside zones on the visual part. For example : [b;block=tr] defines a zone named "b" and limited by the "tr" tags that surrounds it. Thus, templates files don't need coded loops or any procedural syntax inside, they can be created by authoring tools and can be w3c valid. Previewing templates in browsers are figuring something out. This kind of template gives more possibilities : Open Office, Excel, ... TinyButStrong doesn't work by preassigning data, it merges them on demand or sometimes automatically. Keeping the idea of being simple, the engine is not a framework or a language but just a brick in your application. It's only one file, one class with 6 methods and 5 properties. It also accepts plug-ins. It can do as much as serious template engines and is already widely used.

Example of natural template (in wysiwyg):
Names Phones
[users.name;block=tr] [users.phone]
[users.name;block=tr] [users.phone]

~~~ heavy classic musical conclusion ~~~

Feel free to send your comments to the author.