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 (gettext)
 * Macros
 * Fully extendable

Twig is supported by the following integrated development environments:
 * Eclipse via the Twig plugin
 * Komodo and Komodo Edit via the Twig highlight/syntax check mode
 * NetBeans via the Twig syntax plugin (until 7.1, native as of 7.2)
 * PhpStorm (native as of 2.1)
 * IntelliJ IDEs, including WebStorm, via a plugin

And the text editors:
 * Atom via the PHP-twig for atom
 * Emacs via web-mode.el
 * Notepad++ via the Notepad++ Twig Highlighter
 * Sublime Text via the Twig bundle
 * TextMate via the Twig bundle
 * Vim via the Jinja syntax plugin or the vim-twig plugin
 * Brackets via Brackets Twig
 * Visual Studio Code via the Twig extension
 * GTKSourceView via the Twig language definition
 * Coda via the Twig syntax mode
 * Coda 2 via the other Twig syntax mode
 * SubEthaEdit via the Twig syntax mode

Syntax
Twig defines three kinds of delimiters:
 * , to print the content of variables or the result of evaluating an expression (e.g.: an inherited Twig template with ).
 * , 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:

Filters
The filters provide some treatments on an expression, when placed after it, separated by pipes. For example:
 * : changes a string's first letter to capital.
 * : changes a whole string to capital.
 * : displays the first line of an array.
 * : returns a variable size.

Special variables

 * contains the current loop information. For example  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:
 * The CGI environment variables, such as.

Example
The example below demonstrates some basic features of Twig.