Twig (template engine)


Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product 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.
The same template language is used by the Nunjucks template engine, thus Nunjucks is also supported by the following tools.

Features

  • Complex control flow
  • Automatic escaping
  • Template inheritance
  • Variable filters
  • i18n support
  • Macros
  • Fully extendable
Twig is supported by the following integrated development environments:
And the text editors:

Syntax

Twig defines three kinds of delimiters:, to print the content of variables or the result of evaluating an expression., to add comments in the templates. These comments aren't included in the rendered page., to execute statements, such as for-loops.
  • * , to assign.
  • * ... : condition.
  • * ... : counter in a loop.
The apostrophe is the escape character.
To create an iterative array:


An associative array:


Operators precedence

The operators precedence is, from the less to more priority:
OperatorRole
orOr
xorXor
andAnd
b-orBitwise OR
b-xorBitwise XOR
b-andBitwise AND
Is equal?
!=Is different?
<Inferior
>Superior
>=Superior or equal
<=Inferior or equal
inInto
matchesCorresponds
starts withBegins by
ends withFinishes by
..Sequence
+Plus
-Less
~Concatenation
*Multiplication
/Division
//Division rounded to lower
%Modulo
isTest
**Power
|Filter
Array entry
.Attribute or method from an object

Filters

The filters provide some treatments on an expression, when placed after it, separated by pipes. For example: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

loop contains the current loop information. For example loop.index corresponds to the number of iterations which have already occurred.

Example

The example below demonstrates some basic features of Twig.


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