Twig (template engine)
{{Infobox software
| name = Twig
| author = Armin Ronacher,{{Cite web|url=https://github.com/mitsuhiko/twig|title=mitsuhiko/twig|date=August 13, 2019|website=GitHub}} Fabien Potencier
| developer = [https://symfony.com Symfony SAS]
| released = {{release date|2009|10|12}}
| latest release version = {{wikidata|property|reference|P348}}
| latest release date = {{start date and age|{{wikidata|qualifier|P348|P577}}}}
| programming language = PHP
| operating system = Cross-platform
| genre = Template engine
| license = BSD License
| website = {{URL|https://twig.symfony.com}}
}}
Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates.{{Cite web|url=https://twig.symfony.com/doc/3.x/templates.html|title=Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine|website=Twig}} It's an open source product{{Cite web|url=https://github.com/twigphp/Twig|title=twigphp/Twig|date=July 1, 2020|website=GitHub}} 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.{{Cite web|url=http://docs.symfony-reloaded.org/?page=learn|title=Symfony2 Documentation — Documentation|date=August 5, 2010|archive-url=https://web.archive.org/web/20100805001936/http://docs.symfony-reloaded.org/?page=learn|archive-date=2010-08-05|website=Twig}}
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{{Cite web|url=https://twig.symfony.com/doc/3.x/filters/index.html|title=Filters - Documentation - Twig - The flexible, fast, and secure PHP template engine|website=Twig}}
- i18n support (gettext)
- Macros
- Fully extendable{{Cite web|url=https://twig.symfony.com/doc/3.x/advanced.html|title=Extending Twig - Documentation - Twig - The flexible, fast, and secure PHP template engine|website=Twig}}
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{{ ... }}
).{{ parent() }} {# ... #}
, to add comments in the templates. These comments aren't included in the rendered page.{% ... %}
, to execute statements, such as for-loops.{% set foo = 'bar' %} , to assign.{{Cite web|url=https://twig.symfony.com/doc/2.x/tags/set.html|title=set - Documentation - Twig - The flexible, fast, and secure PHP template engine|website=Twig}}{% if i is defined and i == 1%} ... {% endif %} : condition.{% for i in 0..10 %} ... {% endfor %} : counter in a loop.
The apostrophe (') is the escape character.
To create an iterative array:
{% set myArray = [1, 2] %}
An associative array:
{% set myArray = {'key': 'value'} %}
Operators precedence
The operators precedence is, from the less to more priority:
class="wikitable"
! 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 =
= Special variables =
loop
contains the current loop information. For exampleloop.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:
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }} - The CGI environment variables, such as
.{{ app.request.server.get('SERVER_NAME') }}
Example
The example below demonstrates some basic features of Twig.
{% extends "base.html" %}
{% block navigation %}
{% endblock navigation %}
See also
{{Portal|Free and open-source software}}
References
{{Reflist}}
External links
- {{Official website|https://twig.symfony.com|Twig official website}}
- {{Cite web|url=http://fabien.potencier.org/article/34/templating-engines-in-php|accessdate=2023-10-27|title=Templating Engines in PHP|author=Fabien Potencier|date=2009-10-07}}
{{DEFAULTSORT:Twig (Template Engine)}}