Twig
Original authorsArmin Ronacher,[1] Fabien Potencier
DeveloperSymfony SAS
ReleaseOctober 12, 2009 (2009-10-12)
Stable release
3.22.1[2] / 16 November 2025; 7 months ago (16 November 2025)
Written inPHP
Operating systemCross-platform
TypeTemplate engine
LicenseBSD License
Websitetwig.symfony.com
Repository

Twig is a free and open-source template engine for PHP. Its syntax originates from Jinja and Django templates.[3] It is licensed under the BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony comes with bundled support for Twig as its default template engine since version 2.[4]

The same template language is used by the Nunjucks template engine, thus Nunjucks is also supported by the following tools.

Features

edit
  • Complex control flow
  • Automatic escaping
  • Template inheritance
  • Variable filters[5]
  • i18n support (gettext)
  • Macros
  • Fully extendable[3][6]

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

  • Eclipse via the Twig plugin
  • Komodo and Komodo Edit via the Twig highlight/syntax check mode
  • NetBeans via the Twig syntax plugin (until 7.1, native as of 7.2)
  • PhpStorm (native as of 2.1)
  • IntelliJ IDEs, including WebStorm, via a plugin

Twig is supported by the following text editors:

Syntax

edit

Twig defines three kinds of delimiters:

  • {{ ... }} is used to print the content of variables or the result of evaluating an expression.[7]: 585 
  • {# ... #} is used to add comments in the templates. These comments are not included in the rendered page.
  • {% ... %} is used to execute statements, such as for-loops.[7]: 585  For example:
    • {% 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.

Unlike in base PHP, Twig variables do not start with a dollar sign.[7]: 576  The apostrophe (') is the escape character.

To create an iterative array:

{% set myArray = [1, 2] %}

An associative array:

{% set myArray = {'key': 'value'} %}

Operators precedence

edit

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

Operator Role
or Or
xor Xor
and And
b-or Bitwise OR
b-xor Bitwise XOR
b-and Bitwise 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[5]
[] Array entry
. Attribute or method from an object (ex: country.name)

Filters

edit

The filters provide some treatments on an expression, when placed after it, separated by pipes. For example:[5]

  • 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.

Special variables

edit
  • loop contains the current loop information. For example loop.index corresponds to the number of iterations which have already occurred.
  • The global variables begin with underscores. For example:
    • _route (URL part located after the domain)
    • _self (current file name)
    So, to the a page route: {{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
  • The CGI environment variables, such as {{ app.request.server.get('SERVER_NAME') }}.

Example

edit

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 %}&nbsp;&nbsp;{% endif %}
                {{ item.caption|upper }}
            </a>
        </li>
    {% endfor %}
    </ul>
{% endblock navigation %}

Real world usage

edit

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:

  • Drupal adopted Twig as its default template engine starting from version 8, replacing PHPTemplate.
  • CraftCMS uses Twig as its sole templating language, allowing developers to create highly customised front-end experiences.
  • eZ Platform (Ibexa) integrates Twig for rendering templates within its content management system.
  • Bolt CMS uses Twig for theming and front-end rendering.
  • ShopWired uses Twig for theming and front-end rendering.
  • OpenCart uses Twig for theming and HTML rendering.

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.

See also

edit

References

edit
  1. ^ "mitsuhiko/twig". GitHub. August 13, 2019.
  2. ^ "Release 3.22.1". 16 November 2025. Retrieved 18 November 2025.
  3. ^ a b c d "Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  4. ^ "Symfony2 Documentation — Documentation". Twig. August 5, 2010. Archived from the original on 2010-08-05.
  5. ^ a b c "Filters - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  6. ^ "Extending Twig - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  7. ^ a b c Jon Duckett; Emme Stone; Chris Ullman (2022). PHP & MySQL: server-side web development. Hoboken, New Jersey: John Wiley & Sons, Inc. ISBN 978-1-119-14921-7. LCCN 2021951353. Archived from the original on 30 April 2025. Retrieved 28 April 2026.
  8. ^ "set - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
edit

📚 Artikel Terkait di Wikipedia

Web template system

Internet protocol. A template processor (also known as a template engine or template parser) is software designed to combine templates with data (defined

Comparison of web template engines

web template engines used in Web template systems and a brief rundown of their features. Template processor Web template system JavaScript templating Category:Template

Twig (disambiguation)

Twig (template engine), an open source template engine for PHP FKA Twigs, an English singer, songwriter, record producer, director, and dancer Twig (novel)

Drupal

November 2015. This was the first to use Symfony for components and Twig as a template engine and it also used the Composer for managing dependencies. The last

Shopware

its user interface using the Smarty template engine, jQuery, and Ext JS. Since Shopware 6, the Twig template engine is used instead with Bootstrap and

Django (web framework)

metacpan.org". metacpan.org. Symfony. "Home - Twig - The flexible, fast, and secure PHP template engine". twig.sensiolabs.org. Archived from the original

List of PHP software and tools

Textpattern – open-source content management system for PHP and MySQL Twig — template engine TYPO3 – content management framework and content management system

LimeSurvey

better usability the outdated design template system was converted to use the Twig template engine for templates. LimeSurvey is a web application that