Girish Mahajan (Editor)

Twig (template engine)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Developer(s)
  
SensioLabs

Written in
  
PHP

Development status
  
Active

Original author(s)
  
Armin Ronacher, Fabien Potencier

Initial release
  
October 12, 2009 (2009-10-12)

Stable release
  
1.24.0 / January 26, 2016 (2016-01-26)

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product 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.

Contents

Features

  • complex control flow
  • automatic escaping
  • template inheritance
  • variable filters
  • i18n support (gettext)
  • macros
  • fully extendable
  • Twig is supported by the following integrated development environments:

  • Eclipse
  • Komodo
  • NetBeans
  • PhpStorm (natively)
  • Visual Studio
  • And the text editors:

  • Atom
  • emacs
  • Notepad++
  • Sublime Text
  • TextMate
  • vim
  • Syntax

    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 (eg: an inherited Twig template with {{ parent() }}).
  • {# ... #}, which is used to add comments in the templates. These comments aren't included in the rendered page.
  • The apostrophe (') is the escape character.

    The operators precedence is, from the less to more priority:

    Example

    The example below demonstrates some basic features of Twig.

    References

    Twig (template engine) Wikipedia