See our review from 7 of the Best Open Source PHP based template engines.

Top 7: Best Open Source PHP Template Engines

When we talk about template engines in PHP, many developers will tell you that isn't necessary and literally a waste of learning time and resources, because PHP is basically a templat engine itself. However, the PHP syntax didn't evolve in the last years unlike the new proposal of template engines for the language. This is a very critical discussion theme among developers, so it's totally up to you the implementation or usage of a template engine in your PHP project. We can only say one thing, if you are thinking in implementing a template engine in your project, we can recommend you a couple of the best of them.

In this top, we'll share with you 7 of the most imponent PHP based open source template engines.

7. Mustache

Github

Mustache PHP Template Engine

Inspired by ctemplate and et, Mustache is a framework-agnostic way to render logic-free views. As ctemplates says, "It emphasizes separating logic from presentation: it is impossible to embed application logic in this template language."  Think of Mustache as a replacement for your views. Instead of views consisting of ERB or HAML with random helpers and arbitrary logic, your views are broken into two parts: a PHP class and an HTML template. All your logic, decisions, and code is contained in your view. All your markup is contained in your template. The template does nothing but reference methods in your view. This strict separation makes it easier to write clean templates, easier to test your views, and more fun to work on your app's front end. The following snippet provides a preview of how the syntax of Mustaches looks like:

Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}

6. Plates

Github

Plates Open Source PHP Template Engine

Plates is a native PHP template system that’s fast, easy to use and easy to extend. It’s inspired by the excellent Twig template engine and strives to bring modern template language functionality to native PHP templates. Plates is designed for developers who prefer to use native PHP templates over compiled template languages, such as Twig or Smarty. This template engine features:

  • Native PHP templates, no new syntax to learn
  • Plates is a template system, not a template language
  • Plates encourages the use of existing PHP functions
  • Increase code reuse with template layouts and inheritance
  • Template folders for grouping templates into namespaces
  • Data sharing across templates
  • Preassign data to specific templates
  • Built-in escaping helpers
  • Easy to extend using functions and extensions
  • Framework-agnostic, will work with any project
  • Decoupled design makes templates easy to test
  • Composer ready and PSR-2 compliant

The following snippet provides a preview of how the plates template engines syntax looks like:

<?php $this->layout('template', ['title' => 'User Profile']) ?>

<h1>Welcome!</h1>
<p>Hello <?=$this->e($name)?></p>

<h2>Friends</h2>
<ul>
    <?php foreach($friends as $friend): ?>
        <li>
            <a href="/profile/<?=$this->e($friend->id)?>">
                <?=$this->e($friend->name)?>
            </a>
        </li>
    <?php endforeach ?>
</ul>

<?php if ($invitations): ?>
    <h2>Invitations</h2>
    <p>You have some friend invites!</p>
<?php endif ?>

5. Blade

First option Github 

Second option Github

Blade is the simple, yet powerful templating engine provided with Laravel. 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. Blade view files use the .blade.php file extension.

Pitifully, blade is not officially supported by the team of laravel outside of the framework and there are a lot of other public implementations that are supposed to work like the original blade template engine, however not all of them are worthy. We recommend you 2 alternatives for blade that are free (MIT license, i.e. close source/private code is OK) in a single file and without a single dependency of an external library.

4. Volt

Volt is an ultra-fast and designer friendly templating language written in C for PHP. It provides you a set of helpers to write views in an easy way. 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. Therefore many developers will be in familiar territory using the same syntax they have been using with similar template engines. Volt’s syntax and features have been enhanced with more elements and of course with the performance that developers have been accustomed to while working with Phalcon. The following snippet provides a quick preview of how the Volt code looks like:

{# app/views/products/show.volt #}

{% block last_products %}

{% for product in products %}
    * Name: {{ product.name|e }}
    {% if product.status === 'Active' %}
       Price: {{ product.price + product.taxes/100 }}
    {% endif  %}
{% endfor  %}

{% endblock %}

3. Dwoo

Dwoo (/diː-wu:/) (stylized as dwoo) is a modern, flexible and oriented object template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. In many aspects, dwoo is compatible with Smarty's templates and plugins because dwoo's author based it on the general ideas that Smarty has introduced to the world of web development. Started in early 2008 by Jordi Boggiano, the idea came from the fact that Smarty is getting older and older. It carries the weight of it's age, having old features that are inconsistent compared to newer ones, being written for PHP4, it doesn't take advantage of PHP5's more advanced features in the area.

Dwoo's template engine takes advantage of the new features offered by PHP5. So it is a well-written, object-oriented template engine that allows easier and faster development compared with Smarty and still it is compatible enough to allow developers using Smarty to make a smooth transition to dwoo. The following snippet provides a preview of how the Dwoo syntax looks like:

<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <ul id="navigation">
        {foreach $navigation item}
            <li><a href="{$item.href}">{$item.caption}</a></li>
        {/foreach}
        </ul>

        <h1>My Webpage</h1>
        {$a_variable}
    </body>
</html>

2. Smarty

Smarty Templates Open Source PHP Template Engine

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation. Under the hood, 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. This approach keeps the templates easy to maintain, and yet keeps execution times extremely fast. Since the compiled versions are PHP, op-code accelerators such as APC or ZendCache continue to work on the compiled scripts.

Smarty is a template engine, and works as the (V)iew component of your application. Smarty can easily be coupled to any of the engines listed above as the view component. No different than any other software, Smarty has a learning curve. Smarty does not guarantee good application design or proper separation of presentation, this still needs to be addressed by a competent developer and web designer. The following snippet provides a preview of how the smarty syntax looks like:

{include file="header.tpl" title="Info"}

User Information:<p>

Name: {$name|capitalize}<br>
Address: {$address|escape}<br>

{include file="footer.tpl"}

1. Twig

Github

Twig Template Engine for PHP

Twig is a template language for PHP, released under the new BSD license (code and documentation). Twig uses a syntax similar to the Django and Jinja template languages which inspired the Twig runtime environment. 

  • Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
  • Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
  • Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.

Twig supports everything you need to build powerful templates with ease: multiple inheritance, blocks, automatic output-escaping, and much more. The syntax is easy to learn and has been optimized to allow web designers to get their job done fast without getting in their way. The following snippet provides a preview of the syntax of this templat engine:

<div class="form-control">
    <i class="fa fa-calendar"></i> {{ form_label(form.dueDate) }}
    {{ form_widget(form.dueDate) }}

    <small>{{ form_help(form.dueDate) }}</small>

    <div class="form-error">
        {{ form_errors(form.dueDate) }}
    </div>
</div>

Honorable mentions

Latte

Github

Latte PHP Templating Engine

Latte is a template engine for PHP which eases your work and ensures the output is protected against vulnerabilities, such as XSS.

  • Latte is fast: it compiles templates to plain optimized PHP code.
  • Latte is secure: it is the first PHP engine introducing content-aware escaping.
  • Latte speaks your language: it has intuitive syntax and helps you to build better websites easily.

The latte syntax is:

<ul n:if="$items">
{foreach $items as $item}
	<li id="item-{$iterator->counter}">{$item|capitalize}</li>
{/foreach}
</ul>

Documentation can be found on the website.

If you know another awesome PHP based template engine, please share it with the community in the comment box.


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors