Twig | |
Author: | Armin Ronacher,[1] Fabien Potencier |
Developer: | Symfony SAS |
Programming Language: | PHP |
Operating System: | Cross-platform |
Genre: | Template engine |
License: | BSD License |
Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates.[2] It's an open source product[3] 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.[4]
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:[2]
And the text editors:
Twig defines three kinds of delimiters:
<nowiki>{{ ... }}</nowiki>
, to print the content of variables or the result of evaluating an expression (e.g.: an inherited Twig template with <nowiki>{{ parent }}</nowiki>
).{# ... #}
, to add comments in the templates. These comments aren't included in the rendered page.{% ... %}
, to execute statements, such as for-loops.The apostrophe (') is the escape character.
To create an iterative array:
An associative array:
The operators precedence is,[2] 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|-| [] || Array entry|-| . || Attribute or method from an object (ex: country.name )|}FiltersThe filters provide some treatments on an expression, when placed after it, separated by pipes. For example:
Special variables
So, to the a page route:
ExampleThe example below demonstrates some basic features of Twig. See alsoExternal links
|