Twig (template engine)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

Lua error in package.lua at line 80: module 'strict' not found.

Lua error in Module:Infobox at line 199: malformed pattern (missing ']').

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]

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 %}

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

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

See also

  • 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

<templatestyles src="Reflist/styles.css" />

Cite error: Invalid <references> tag; parameter "group" is allowed only.

Use <references />, or <references group="..." />

External links

  • Twig official website
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.