Twig (template engine) explained

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.

Features

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

And the text editors:

Syntax

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,[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)|}

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.
  • 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: <nowiki>{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}</nowiki>

  • The CGI environment variables, such as <nowiki>{{ app.request.server.get('SERVER_NAME') }}</nowiki>.

Example

The example below demonstrates some basic features of Twig.

See also

External links

Notes and References

  1. Web site: mitsuhiko/twig. August 13, 2019. GitHub.
  2. Web site: Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine. twig.symfony.com.
  3. Web site: twigphp/Twig. July 1, 2020. GitHub.
  4. Web site: Symfony2 Documentation — Documentation. August 5, 2010. https://web.archive.org/web/20100805001936/http://docs.symfony-reloaded.org/?page=learn. 2010-08-05.
  5. Web site: Filters - Documentation - Twig - The flexible, fast, and secure PHP template engine. twig.symfony.com.
  6. Web site: Extending Twig - Documentation - Twig - The flexible, fast, and secure PHP template engine. twig.symfony.com.
  7. Web site: set - Documentation - Twig - The flexible, fast, and secure PHP template engine. twig.symfony.com.