What is a template engine?

A template engine is a script that loads templates and draws the graphics of your application. The purpose of the template engine is to separate the logic from the presentation, so developers and designers can work better together, because developers can focus on the PHP and designers can work on the HTML.

here a simple example of how a template engine works:


My name is {$name}


$t=new raintpl;
$t->assign("name", // get the name from a database for example // );
$t->draw( "my_template" ); // draw the template

So this snippet will draw something like

"Hello my name is Rain"

Why should I use a template engine?

As probably all of you knows PHP born as a template engine, during the years he evolved its coding features, but it's exactly the same for the presentation features.

For example the short tag <?= is not enabled by default in all servers, so the syntax <?=$title?> is not compatible everywhere. There's still the
closing-php-tag-eating-new-line "bug", which can become a problem with javascript or in the configuration files. It doesn't exists any official PHP documentation for PHP template designers, don't forget that web designers think HTML , they think totally different by us, PHP developers, so learn how to design PHP template from the huge documentation of php.net can be not that easy!

A template engine also allows to work in a controlled environtment, called sandbox, which allows developers to load templates from untrusted source, because is not possible to execute "dangerous code".

Another last reason is for best practices, separating your project in more layer, helps your team to work better on each single task, and to easily maintain big projects.

What is the best PHP template engine?

The one that fit the best your application needs. There are a few template engines very good, and many offer different interesting features, we advise you to just choose the one you like the most.

What is the faster PHP template engine?

RainTPL is a fast and light template engine, if performance are important for you try RainTPL.

Why Rain?

Rain is a PHP template engine, easy, fast and secure. It is open source under GNU/LGPL, so you are free to use it in your free or commercial projects.

It totally separates logic from presentation, so developers focus on PHP and designers on HTML.

Is the only template engine that uses relative paths. This feature called WYSIWYG allows designers to work on templates, like static page, so it's possible to immediately check the result on the browser. Cool ah?

Why should I use a template engine in an MVC Framework?

Templates keep your presentation layer clean and elegant, which enable your application to be more maintainable than using PHP Views.

Also thanks to the sandbox, you can't run dangerous code in your templates, and you can have sweet dream!

Here a tutorial of how to integrate RainTPL in Zend Framework.
Rain Framework, uses by default Rain TPL for the presentation layer.