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.

Features

Twig is supported by the following integrated development environments:
And the text editors:
Twig defines three kinds of delimiters:
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
b-andBoolean and
b-xorExclusive or
b-orBoolean or
orOr
andAnd
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 place after it, separated by pipes. For example:
The example below demonstrates some basic features of Twig.