Do you remember the excitement you felt the first time you uploaded an HTML page to a Web server? After all, the ability to update a page and make it available to the world in mere moments is a pretty mind-blowing concept, leaving little wonder why the Web has risen to become a global phenomenon in such a short period of time.
I think PHP has become such a popular language because like HTML it also promotes a sense of immediate gratification. It's possible to create a dynamic PHP page despite knowing very little about the language, and in fact you're required to know only what is necessary to achieve the desired effect. While this lack of formality has no doubt played a major role in attracting new developers, many have unfortunately taken to abusing the language, with spaghetti code being the result.
If you've fallen into the trap of mixing your website presentation and logic and are looking for a way out, there are actually quite a few solutions at your disposal. Known as templating engines, these solutions offer the ability to cleanly separate your PHP code from the HTML and other presentational languages (e.g. CSS and JavaScript), improving maintainability and testability.
In this article I'll introduce five of PHP's most popular templating engines, providing you with a basis for continuing your own investigations. Keep in mind however that this list is by no means definitive; if you have experience using a templating engine not discussed here, please tell us about it in the comments!

1. Smarty PHP Template Engine

Smarty is the patriarch PHP templating engine, having been under active development for much of the past 10 years. Used by hundreds of thousands of sites and products around the globe, including Flickr, X-CART, and PrestaShop, Smarty sports a simple and extensible tag syntax which developers can use to inject dynamic content into a template. The dynamic content is passed to a Smarty template by instantiating the Smarty class and using its assign() method to associate dynamic values with placeholders found in the template:

$smarty = new Smarty;

$smarty->assign('games', 45);

The homepage.tpl file looks like this:
<title>A Smarty Demo</title>
<p>Your library consists of {$games} games.</p>
In addition to this tag-based templating feature, Smarty supports conditional and looping syntax, variable modifiers, and can be extended via custom functions and plugins. Learn more about Smarty at

2. Dwoo PHP Template Engine

Dwoo is one of the more recent PHP templating engines, having been created in 2008 in part out of the desire to take advantage of some of the object-oriented capabilities made available with PHP 5. Although only three years old, Dwoo is already seeing widespread adoption, and is used by the TYPO3 CMS among other high profile projects.
Like Smarty, Dwoo supports template inheritance, giving you the ability to easily override default template values. For instance you might wish to define a default page header and then override it in certain circumstances, such as when the user navigates to the website's about page. Begin by defining a default layout layout.html):
<title>Doing It With Dwoo</title>
<h1>{block "title"}Welcome to My Site{/block}</h1>
You can then define an about-page specific layout (about.html) which inherits from layout.phtml and overrides the title block default:
{extends "layout.html"}
{block "title"}About Us{/block}
Learn more about Dwoo at

3. Twig PHP Template Engine

Twig is a templating engine solution created by Sensio Labs, the brilliant company behind the symfony framework. As is typical of all Sensio Labs-driven projects, Twig is accompanied by impressive user manual, and considerable thought has been put into allowing developers ample opportunities to extend the engine.
One particularly interesting Twig feature is its sandbox extension. This extension can be used to safely evaluate untrusted code should you allow users to create Twig-based templates, as might be the case were you to create for instance a blogging service.

4. Savant3 PHP Template Engine

Savant3 differs from the other templating solutions introduced in this article in that it does not require users to learn a new syntax, instead using PHP as the templating language. You might wonder whether this defeats the stated purpose of separating PHP logic from the website's presentation, however by distilling the PHP code down to a few presentation-specific methods you are effectively accomplishing the same goals set forth by the competing solutions, while simultaneously saving yourself the trouble of learning an engine-specific syntax. For example the following PHP script will ready a template for outputting an array of video games:
require_once 'Savant3.php';

$template = new Savant3;

$games = array (
  array (
    'title' => 'Call of Duty: Black Ops'.
    'esrb'  => 'Mature'
  array (
    'title' => 'LittleBigPlanet 2'.
    'esrb'  => 'Everyone'

$template->games = $games;

Next define the template (games.tpl.php):
<?php foreach ($this->books AS $key => $value) { ?>
<h3><?php echo $this->eprint($value['title']); ?></h3>
ESRB: <?php echo $this->eprint($value['esrb']); ?>
<?php } ?>
This syntax probably looks quite familiar, save for perhaps the reference to the eprint() method. This method is a Savant3 method which will automatically escape the output before inserting it into the template, lessening the possibility of cross-site scripting attacks.
Learn more about Savant3 at

5. PHPTal PHP Template Engine

PHPTal is a PHP-based implementation of Zope Page Templates, a Web page generation tool used by the Python-based Web application server Zope. PHPTal differs from the other templating engines discussed in this article in that it implements an XML-based approach. For instance, the following PHPTal template is used to iterate over an array:
<div tal:repeat="game games">
<h3 tal:replace="game/getTitle">
<p tal:replace="game/getDescription">
Executing this template produces output which looks like this:
<h3>Call of Duty: Black Ops</h3>
The latest release in the bestselling first-person shooter series.
<h3>Pac-Man Championship Edition DX</h3>
Pac-Man Championship Edition DX introduces new twists and challenges into
a game we've grown to love.
Learn more about PHPTal at


Are you using a PHP templating engine not included in this list? Tell us about it in the comments!

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.