#native_company# #native_desc#

5 Popular PHP Template Engines Worth Checking Out

By W. Jason Gilmore
on April 7, 2011

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 smarty.net.

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 dwoo.org.

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.