Twig (template engine)

Last updated
Twig
Original author(s) Armin Ronacher, [1] Fabien Potencier
Developer(s) Symfony SAS
Initial releaseOctober 12, 2009 (2009-10-12)
Stable release
3.21.1 [2] / 3 May 2025;4 months ago (3 May 2025)
Repository
Written in PHP
Operating system Cross-platform
Type Template engine
License BSD License
Website twig.symfony.com

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]

Contents

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

Features

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

And the text editors:

Syntax

Twig defines three kinds of delimiters:

The apostrophe (') is the escape character.

To create an iterative array:

{%setmyArray=[1,2]%}

An associative array:

{%setmyArray={'key':'value'}%}

Operators precedence

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

OperatorRole
b-and Bitwise AND
b-xor Bitwise XOR
b-or Bitwise OR
orOr
andAnd
==Is equal?
!=Is different?
<Inferior
>Superior
>=Superior or equal
<=Inferior or equal
inInto
matchesCorresponds
starts withBegins by
ends withFinishes by
..Sequence (ex: 1..5)
+Plus
-Less
~Concatenation
*Multiplication
/Division
//Division rounded to lower
%Modulo
isTest (ex: is defined or is not empty)
**Power
|Filter [6]
[]Array entry
.Attribute or method from an object (ex: country.name)

Filters

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

Special variables

Example

The example below demonstrates some basic features of Twig.

{%extends"base.html"%}{%blocknavigation%}<ulid="navigation">{%foriteminnavigation%}<li><ahref="{{item.href}}">{%ifitem.level==2%}&nbsp;&nbsp;{%endif%}{{item.caption|upper}}</a></li>{%endfor%}</ul>{%endblocknavigation%}

Real world usage

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.

See also

References

  1. "mitsuhiko/twig". GitHub. August 13, 2019.
  2. "Release 3.21.1". 3 May 2025. Retrieved 1 June 2025.
  3. 1 2 3 4 "Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  4. "twigphp/Twig". GitHub. July 1, 2020.
  5. "Symfony2 Documentation — Documentation". Twig. August 5, 2010. Archived from the original on 2010-08-05.
  6. 1 2 3 "Filters - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  7. "Extending Twig - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  8. "set - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.