Top Qs
Timeline
Chat
Perspective
Twig (template engine)
From Wikipedia, the free encyclopedia
Remove ads
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.
Remove ads
Features
- Complex control flow
- Automatic escaping
- Template inheritance
- Variable filters[5]
- i18n support (gettext)
- Macros
- Fully extendable[2][6]
Twig is supported by the following integrated development environments:[2]
- 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
Remove ads
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.[7]{% 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'} %}
Remove ads
Operators precedence
Summarize
Perspective
The operators precedence is,[2] from the less to more priority:
Filters
The filters provide some treatments on an expression, when placed after it, separated by pipes. For example:[5]
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
loopcontains the current loop information. For exampleloop.indexcorresponds 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 %}
<ul id="navigation">
{% for item in navigation %}
<li>
<a href="{{ item.href }}">
{% if item.level == 2 %} {% endif %}
{{ item.caption|upper }}
</a>
</li>
{% endfor %}
</ul>
{% endblock navigation %}
Remove ads
Real world usage
Twig has become a widely adopted templating engine in the PHP ecosystem. It is the default templating language for the Symfony framework and is also integrated into several content management systems (CMS) and applications, including:
- Drupal adopted Twig as its default template engine starting from version 8, replacing PHPTemplate.
- CraftCMS uses Twig as its sole templating language, allowing developers to create highly customised front-end experiences.
- eZ Platform (Ibexa) integrates Twig for rendering templates within its content management system.
- Bolt CMS uses Twig for theming and front-end rendering.
- ShopWired uses Twig for theming and front-end rendering.
Twig is also employed in many custom PHP applications where secure and readable templating are important. The similarity of Twig to Jinja and Django templates has also made it familiar to developers coming from Python and other similar ecosystems.
Remove ads
See also
References
External links
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads