Twig (template engine)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates.[1] It's an open source product[2] licensed under a BSD License and developed by Fabien Potencier. Symfony2 PHP framework comes with a bundled support for Twig as its default template engine.[3]

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

Twig defines three kinds of delimiters:

  • {% ... %}, which is used to execute statements, such as for-loops.
  • {{ ... }}, which is used to print the content of variables or the result of evaluating an expression.
  • {# ... #}, which is used to add comments in the templates. These comments aren't included in the rendered page.

Features[edit]

  • complex control flow
  • automatic escaping
  • template inheritance
  • variable filters
  • i18n support (gettext)
  • macros
  • fully extendable[1][4]

See also[edit]

References[edit]

External links[edit]