Twig (template engine)

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. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2.[5]

Twig
Original author(s)Armin Ronacher,[1] Fabien Potencier
Developer(s)SensioLabs
Initial releaseOctober 12, 2009 (2009-10-12)
Stable release
3.2.1[2] / 5 January 2021 (5 January 2021)
Repository
Written inPHP
Operating systemCross-platform
TypeTemplate engine
LicenseBSD License
Websitetwig.symfony.com

Features

  • 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:[3]

And the text editors:

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.[8]
    • {% 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,[3] from the less to more priority:

OperatorRole
b-andBitwise AND
b-xorBitwise XOR
b-orBitwise OR
orOr
andAnd
==Is equal?
!=Is different?
<Inferior
>Superior
>=Superior or equal
<=Inferior or equal
inInto
matchesCorresponds
starts withBegins by
ends withFinishes by
..Sequence (ex: 1..5)
+Plus
-Less
~Concatenation
*Multiplication
/Division
//Division rounded to lower
%Modulo
isTest (ex: is defined or is not empty)
**Power
|Filter[6]
[]Array entry
.Attribute or method from an object (ex: country.name)

Filters

The filters provide some treatments on an expression, when place after it, separated by pipes. For example:[6]

  • 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: {{ 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 %}&nbsp;&nbsp;{% endif %}
                {{ item.caption|upper }}
            </a>
        </li>
    {% endfor %}
    </ul>
{% endblock navigation %}

See also

References

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.