Skip navigation

Lost password? | Help

Hello World

Practical programming... and stuff...

The simplest PHP template engine

,

If you have done more PHP programming you've probably used a template engine or two. I personally prefer Smarty as my template engine of choice for most projects.

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.

What to do with a web camera?Collecting email addresses from websites

Comments

mank1 18. July 2007, 06:40

I was working with Smarty for a while last year, then got introduced to Code Igniter. The end result with that was closer to your raw example (which is cooler, in my opinion). Check CI out some time if you haven't already. It's pretty neat.

Jani 18. July 2007, 21:47

Personally I prefer Smarty's way of doing the templates as it's much cleaner and looks nicer imo. With PHP you need at least 3 characters more if you want to print a variable and in reality you'll probably need 9 because you need to use the normal long tags + echo as the short open tags aren't always enabled. Plus, I've found out that Smarty's tags look less confusing for people who can't really program PHP.

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

Rasmus Schultz writes:

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.

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies