The simplest PHP template engine
Sunday, 15. July 2007, 20:21:53
If you don't know what a template engine is, I'll give a short explanation: Template engine is something that can be used to help separate HTML from PHP - You do all your "business logic" in a PHP file, for example you fetch some things from a database. A template on the other hand is meant to display that data. This is usually achieved by assigning the data to template variables in PHP and then displaying the template file.
But most template engines need setting up and their own code base etc., so for small projects they might add unnecessary complexity and things like that. However, PHP on its own can be a simple template engine! In this post I'll show you how.
Let's take a simple example. Say we want to display a list of members in our database on a page. I'm going to use Smarty as an example here.
First, you'd write the code to get the members list from the database and assign it to your template:
memberlist.php
<?php
$members = $memberManager->GetAll();
$smarty->assign('members',$members);
$smarty->show('member_list.tpl');
?>
member_list.tpl
<table>
{foreach from=$members var=member}
<tr>
<td>{$member.firstname}</td>
<td>{$member.lastname}</td>
</tr>
{/foreach}
</table>
So with Smarty you could do it like that, but how about without Smarty (or any other template engine)?
You could throw the HTML code in the middle of the PHP, but that's not nice...
This is how you would use PHP itself as a template engine:
memberlist.php
<?php $members = $memberManager->GetAll(); include 'templates/member_list.phtml'; ?>
member_list.phtml
<table> <?php foreach($members as $member): ?> <tr> <td><?php echo $member['firstname']; ?></td> <td><?php echo $member['lastname']; ?></td> </tr> <?php endforeach; ?> </table>
If you assign a variable in PHP and then include an another file like that, the included file can use the variables used in the code that included it. This way you can assign "template variables".
But what's with the odd syntax for foreach? This is PHP's alternative syntax for control structures. PHP can use a Visual Basic -like syntax instead of the typical C-style syntax and at least I think that the VB-like syntax is more clear when being mixed like that with HTML.
So that is how you use PHP as a template engine without any "outside" libraries. Neat huh? Sure, the syntax is not nearly as clean and short as in Smarty, but it's not that bad either.
Also, if you enable PHP's short tags, you can use <?= $member['firstname']; ?> instead of the longer one with the echo statement which makes the code a bit cleaner. You just have to remember that not all servers have short tags enabled.







mank1 # 18. July 2007, 06:40
Jani # 18. July 2007, 21:47
What comes to CodeIgniter itself, I've looked into it before and some other frameworks such as Zend's one, and I find Zend Framework a more... "complete" solution. Maybe it's because ZF does not work in PHP4 so it can fully utilize PHP5 features... don't know, but it feels better.
Anonymous # 13. March 2008, 16:51
I too prefer the legible Smarty-style syntax, but I found a lot of Smarty's syntax to be far too long, and the overhead of the engine itself to be unacceptable, considering what it actually does.
So I want ahead and wrote my own template engine:
http://outline.mindplay.dk
The syntax borrows from Smarty, so it's just as legible, but most of the time it's much shorter. It lets you freely use all standard PHP functions, operators, classes, etc. but it simplifies the syntax, so that non-programmers won't have to see a lot of strange brackets and paranthesis everywhere.
The entire engine is less than 1000 lines of code - and only about 200 lines for templates that have already been compiled.