Jump to content

Twig (template engine): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Added original author for the first release
Line 3: Line 3:
{{Infobox software
{{Infobox software
| name = Twig
| name = Twig
| author = Fabien Potencier
| author = Armin Ronacher<ref>[https://github.com/mitsuhiko/twig Original Repository]</ref>, Fabien Potencier
| developer = [http://sensiolabs.com/ SensioLabs]
| developer = [http://sensiolabs.com/ SensioLabs]
| released = {{release date|2009|10|12}}
| released = {{release date|2009|10|12}}
Line 16: Line 16:
}}
}}


'''Twig''' is a [[template engine]] for the [[PHP|PHP programming language]]. Its syntax originates from [[Jinja (template engine)|Jinja]] and [[Django (web framework)|Django]] templates.<ref name="twigfortemplatedesigners">[http://twig.sensiolabs.org/doc/templates.html Twig documentation for template designers]</ref> It's an open source product<ref>[https://github.com/fabpot/Twig Twig repository on GitHub]</ref> licensed under a [[BSD licenses|BSD License]] and developed by Fabien Potencier. [[Symfony|Symfony2]] PHP framework comes with a bundled support for Twig as its default template engine.<ref>[http://docs.symfony-reloaded.org/?page=learn Symfony2 Documentation]</ref>
'''Twig''' is a [[template engine]] for the [[PHP|PHP programming language]]. Its syntax originates from [[Jinja (template engine)|Jinja]] and [[Django (web framework)|Django]] templates.<ref name="twigfortemplatedesigners">[http://twig.sensiolabs.org/doc/templates.html Twig documentation for template designers]</ref> It's an open source product<ref>[https://github.com/fabpot/Twig Twig repository on GitHub]</ref> licensed under a [[BSD licenses|BSD License]] and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. [[Symfony|Symfony2]] PHP framework comes with a bundled support for Twig as its default template engine.<ref>[http://docs.symfony-reloaded.org/?page=learn Symfony2 Documentation]</ref>


== Example ==
== Example ==

Revision as of 19:14, 23 March 2015

Twig
Original author(s)Armin Ronacher[1], Fabien Potencier
Developer(s)SensioLabs
Initial releaseOctober 12, 2009 (2009-10-12)
Stable release
1.18[2] / February 1, 2015 (2015-02-01)
Repository
Written inPHP
Operating systemCross-platform
TypeTemplate engine
LicenseBSD License
Websitetwig.sensiolabs.org

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

  • 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

  • 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.