Twig (template engine)
| Original author(s) | Armin Ronacher,[1] Fabien Potencier |
|---|---|
| Developer(s) | SensioLabs |
| Initial release | October 12, 2009 |
| Stable release |
1.24.0[2] / January 26, 2016
|
| Development status | Active |
| Written in | PHP |
| Operating system | Cross-platform |
| Type | Template engine |
| License | BSD License |
| Website | twig |
Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates.[3] It's an open source product[4] licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony2 PHP framework comes with a bundled support for Twig as its default template engine.[5]
Contents
Features[edit]
- complex control flow
- automatic escaping
- template inheritance
- variable filters[6]
- i18n support (gettext)
- macros
- fully extendable[3][7]
Twig is supported by the following integrated development environments:[8]
- Eclipse
- Komodo
- NetBeans
- PhpStorm (natively)
- Visual Studio
And the text editors:
Syntax[edit]
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.[9]{% 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.
Operators precedence[edit]
The operators precedence is,[10] from the less to more priority:
| Operator | Role |
|---|---|
| b-and | Boolean and |
| b-xor | Exclusive or |
| b-or | Boolean 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[6] |
| [] | Array entry |
| . | Attribute or method from an object (ex: country.name) |
Filters[edit]
The filters provide some treatments on an expression, when place after it, separated by pipes. For example: [6]
capitalize: changes a string first letter in capital.upper: changes a while string in capital.first: displays the first line of an array.length: returns a variable size.
Special variables[edit]
loopcontains the current loop information. For exampleloop.indexcorresponds to the number of iterations which have already occurred.- The global variables begin by 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[edit]
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 %}
See also[edit]
- Smarty
- Twital, a template engine that compile its sources into Twig templates, adding some features as context-aware escaping, attribute-expression and creating more readable templates.
References[edit]
- ^ Original Repository
- ^ [1]
- ^ a b Twig documentation for template designers
- ^ Twig repository on GitHub
- ^ Symfony2 Documentation
- ^ a b c https://twig.symfony.com/doc/filters/index.html
- ^ Extending Twig
- ^ https://twig.symfony.com/doc/templates.html#ides-integration
- ^ https://twig.symfony.com/doc/2.x/tags/set.html
- ^ https://twig.symfony.com/doc/templates.html
External links[edit]
- Twig official website
- Potencier, Fabien (October 7, 2009). "Templating Engines in PHP". Retrieved 6 April 2011.
- Potencier, Fabien (November 20, 2009). "Templating Engines in PHP (Перевод: Шаблонизаторы в PHP)" (in Russian). Retrieved 6 April 2011.