Twig (template engine)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Original author(s) Armin Ronacher,[1] Fabien Potencier
Developer(s) SensioLabs
Initial release October 12, 2009 (2009-10-12)
Stable release
1.24.0[2] / January 26, 2016 (2016-01-26)
Development status Active
Written in PHP
Operating system Cross-platform
Type Template engine
License BSD License

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates.[3] It's an open source product[4] licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony2 PHP framework comes with a bundled support for Twig as its default template engine.[5]


  • complex control flow
  • automatic escaping
  • template inheritance
  • variable filters
  • i18n support (gettext)
  • macros
  • fully extendable[3][6]

Twig is supported by the following integrated development environments:[7]

And the text editors:


Twig defines three kinds of delimiters:

  • {% ... %}, which is used to execute statements, such as for-loops.
  • {{ ... }}, which is used to print the content of variables or the result of evaluating an expression (eg: an inherited Twig template with {{ parent() }}).
  • {# ... #}, which is used to add comments in the templates. These comments aren't included in the rendered page.

The apostrophe (') is the escape character.

The operators precedence is,[8] from the less to more priority:

Operator Role
b-and Boolean and
b-xor Exclusive or
b-or Boolean or
or Or
and And
== Is equal?
 != Is different?
< Inferior
> Superior
>= Superior or equal
<= Inferior or equal
in Into
matches Corresponds
starts with Begins by
ends with Finishes by
.. Sequence (ex: 1..5)
+ Plus
- Less
~ Concatenation
* Multiplication
/ Division
// Division rounded to lower
 % Modulo
is Test (ex: is defined or is not empty)
** Power
| Filter[9]
[] Array entry
. Attribute or method from an object (ex:


The example below demonstrates some basic features of Twig.

{% extends "base.html" %}
{% block navigation %}
    <ul id="navigation">
    {% for item in navigation %}
            <a href="{{ item.href }}">
                {% if item.level == 2 %}&nbsp;&nbsp;{% endif %}
                {{ item.caption|upper }}
    {% endfor %}
{% endblock navigation %}

See also[edit]

  • Smarty
  • Twital, a template engine that compile its sources into Twig templates, adding some features as context-aware escaping, attribute-expression and creating more readable templates.


External links[edit]