This article will address Twig (template engine) from different perspectives, in order to offer readers a comprehensive and detailed view on this topic. Relevant aspects will be analyzed, relevant data will be presented and various opinions from experts in the field will be offered. Twig (template engine) is a topic that arouses great interest and curiosity in today's society, so it is essential to delve into its study to understand its importance and impact in different areas. Throughout this article, different facets of Twig (template engine) will be explored, with the purpose of providing readers with a complete and enriching overview of this topic.
| Twig | |
|---|---|
| Original authors | Armin Ronacher,[1] Fabien Potencier |
| Developer | Symfony SAS |
| Initial release | October 12, 2009 |
| Stable release | 3.22.1[2]
/ 16 November 2025 |
| Repository | |
| Written in | PHP |
| Operating system | Cross-platform |
| Type | Template engine |
| License | BSD License |
| Website | twig |
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. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2.[5]
The same template language is used by the Nunjucks template engine, thus Nunjucks is also supported by the following tools.
Twig is supported by the following integrated development environments:[3]
And the text editors:
Twig defines three kinds of delimiters:
{{ ... }}, to print the content of variables or the result of evaluating an expression (e.g.: an inherited Twig template with {{ parent() }}).{# ... #}, to add comments in the templates. These comments aren't included in the rendered page.{% ... %}, to execute statements, such as for-loops.
{% set foo = 'bar' %}, to assign.[8]{% if i is defined and i == 1%} ... {% endif %}: condition.{% for i in 0..10 %} ... {% endfor %}: counter in a loop.The apostrophe (') is the escape character.
To create an iterative array:
{% set myArray = %}
An associative array:
{% set myArray = {'key': 'value'} %}
The operators precedence is,[3] from the less to more priority:
| Operator | Role |
|---|---|
| b-and | Bitwise AND |
| b-xor | Bitwise XOR |
| b-or | Bitwise 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[6] |
| Array entry | |
| . | Attribute or method from an object (ex: country.name)
|
The filters provide some treatments on an expression, when placed after it, separated by pipes. For example:[6]
capitalize: changes a string's first letter to capital.upper: changes a whole string to capital.first: displays the first line of an array.length: returns a variable size.loop contains the current loop information. For example loop.index corresponds to the number of iterations which have already occurred.{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}{{ app.request.server.get('SERVER_NAME') }}.The example below demonstrates some basic features of Twig.
{% extends "base.html" %}
{% block navigation %}
<ul id="navigation">
{% for item in navigation %}
<li>
<a href="{{ item.href }}">
{% if item.level == 2 %} {% endif %}
{{ item.caption|upper }}
</a>
</li>
{% endfor %}
</ul>
{% endblock navigation %}
Twig has become a widely adopted templating engine in the PHP ecosystem. It is the default templating language for the Symfony framework and is also integrated into several content management systems (CMS) and applications, including:
Twig is also employed in many custom PHP applications where secure and readable templating are important. The similarity of Twig to Jinja and Django templates has also made it familiar to developers coming from Python and other similar ecosystems.