6 Best Template Engines You Can Use in PHP [updated]

By: Sunil Kumar |  In: PHP  |  Last Updated: 2018/12/29

6 Best Template Engines You Can Use in PHP [updated]

The first thing you might be thinking –

Why a templating engine when there is PHP as a templating engine?

Yes, you can use PHP as a templating language But why we do this if there are more reliable and more lightweight options are available.

For example, for displaying the value of a variable in PHP, you will write –

<?php echo $var; ?>

But same can be done with lesser code in other templating languages like Twig, Django just by writing –

  {{ var }}

PHP is best suited for the Business logic. It should not be mixed with Presentation Layer.

The business logic of an application determines how data are created, displayed, stored and changed. Presentation layer determines how data are presented to the user in a certain format such as HTML, JSON, XML or some other.

Instead of mixing your application’s business logic and presentation and duplicating code, best practice is to separate that with templates.

Personally, I don’t consider PHP as a templating language because I am unable to find any good change in PHP which enhanced PHP as a templating language.

For example, Django introduced template inheritance some years ago, as a way to mimic classes but for templates:

 {% extends "base.html" %}

  {% block header%}
  {% endblock}
  {% block body%}
  {% endblock}

  {% block footer%}
  {% endblock}

It’s elegant, easy to understand, and really powerful. It’s so powerful that many template engines now support this feature out of the box.

As we continue to develop complex web applications which are modelled after the MVC architecture, It is becoming more important to have a wall of separation between back-end logic and front-end logic so that back-end developers and front-end developers can collaborate on the same areas of the website without having to step around each other’s code.

Sandbox mode(To be noted)

This one is a must-have when you allow users to edit templates (when webmasters are allowed to edit some templates from a web backend for instance). This is not a universal need, but one that needs to be addressed anyway. Evaluating a template in a sandbox means being able to restrict what can be done in it.

For instance, you should be able to restrict the methods/functions that can be called, the tags that can be used and much more.This features missing in PHP.

Alternative template engines in PHP

So I started looking for other engines which are designed especially for templating and I found a list of them. But all are not good templating engines. I read many articles, compared them on the basic of features and figured top 6 most widely used templating engines.

1. Twig

Twig was written by Armin Ronacher of Jinja fame. It is one of the most popular templating engines. Despite being one the most full-featured PHP templating engines.

Despite being one of the most full-featured PHP templating engines, Twig is also the fastest one.

Twig was my first templating engine. The first time I used it in 2014 and since then I am loving it.

Some of the top features of twig-

  • Native template inheritance (templates are compiled as classes).
  • Solid automatic auto-escaping (with no associated runtime overhead as everything is done during compilation).
    Very secure sandbox mode which allows Twig to be used as a template language for applications where users may modify the template design.
  • Great extensibility: you override everything, even the core features, by bundling your own tags and filters as an extension; but you can also manipulate the AST (Abstract Syntax Tree) before compilation. By leveraging these possibilities, you can even create your own DSL (Domain Specific Language), targeted at your application.
  • Template oriented syntax: Twig has shortcuts for common patterns, like having a default text displayed when you iterate over an empty array.
  • Full Featured: Twig supports everything you need to build powerful templates with ease: multiple inheritance, blocks, automatic output-escaping, and much more.
  • Clean Error Messages: Whenever you have an error in your twig template, twig error messages are so specific you can easily find where is the problem.

2. Smarty

Recently Smarty has been changing and adapting aggressively to better suit everyone’s needs and take advantage of new things in PHP core. As the twig, Smarty focuses on the templating and lesser the code. Smarty uses simple syntax {tag} which is designed especially for presentation.

Smarty provides a crash course where you can start with it. Smarty compiles copies of the templates as PHP scripts. This way you get the benefits of both template tag syntax and the speed of PHP.

Compilation happens once when each template is first invoked, and then the compiled versions are used from that point forward. Smarty takes care of this for you, so the template designer just edits the Smarty templates and never has to manage the compiled versions.

Smarty also uses the concept of sandboxing.

In Smarty 3 they have introduced Inheritance. And this made Smarty smarter. Using Inheritance you can easily manipulate blocks by inheriting them.

3. Mustache

Mustache can be used for HTML, config files, source code – anything. It works by expanding tags in a template using values provided in a hash or object.

We call it “logic-less” because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values.

4. Blade

Blade is the simple, yet powerful templating engine provided as a default templating engine with Laravel.  It’s provided with Laravel but that does not mean that you can’t use it with other frameworks.

It is a stand-alone component and you can use it independently. Blade was released in 2011 and risen so fast that it is one of the most used templating engines in PHP.

Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.

But the main problem with the blade is, documentation of blade is not so much rich. It is missing some important point from its documentation.

5. Volt

Volt is an ultra-fast and designer-friendly templating language written in C for PHP. Volt is highly integrated with other components of Phalcon, just as you can use it as a stand-alone component in your applications.

Volt is inspired by Jinja, originally created by Armin Ronacher. The syntax of volt is very similar to twig syntax. volt has a very clean, informative documentation.

But the main fallback of volt is you can’t decouple it from Phalcon entirely. So if you want to use volt with other frameworks, you can’t. You could fall back on the Twig templating engine which seems to be the closest match language wise as well as completely standalone.

6. Dwoo

Dwoo is a good alternative to Smarty. And the did a great job. Like twig and Smarty, Dwoo supports template inheritance, giving you the ability to easily override default template values. But unfortunately, Dwoo has no sandbox feature and its core is not flexible enough.

Key features-

  • Generally faster than Smarty.
  • Flexible plugin creation
  • Smarty syntax compatibility with several additions and shortcuts to speed up template writing

Also Read:

How You can write Super-Fast HTML with Sublime text


As a framework-independent technology, Twig is a templating engine which can be adapted easily and independently. Twig is feature-rich, easily customizable and perfectly documented.

I am feeling great by using it. And if you are familiar with other templating engines you can easily start with twig because the syntax is similar to other engines.


  • Smarty is pretty dumb and needs arrays as input. There is no automatic search for getters and objects not usable. Concerning any framwork DTOs seem being required if you want to write clean code. Haven’t tested the rest of this list, so I can’t compare here.
    Using fluid (for Flow, Neos and TYPO3) I know a much more comfortable template engine.

  • Hi Sunil, good article, thanks for sharing. One remark: Volt can be used in standalone mode, the only Phalcon dependency is the declaration of the DI, but once it is registered, you can send any data to the template. It’s very fast.

  • Leave a Comment

    Your email address will not be published.


    %d bloggers like this: