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.
Symfony
Symfony is a free and open-source PHP web application framework and a set of reusable PHP component libraries. It was published as free software on October 18, 2005, and released under the MIT license.
Goal
Symfony aims to speed up the creati ...
PHP framework comes with a bundled support for Twig as its default template engine since version 2.
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
An eclipse is an astronomical event that occurs when an astronomical object or spacecraft is temporarily obscured, by passing into the shadow of another body or by having another body pass between it and the viewer. This alignment of three ce ...
via the Twig plugin
*
Komodo and
Komodo Edit via the Twig highlight/syntax check mode
*
NetBeans via the Twig syntax plugin (until 7.1, native as of 7.2)
*
PhpStorm (native as of 2.1)
And the text editors:
*
Atom via the PHP-twig for atom
*
Emacs
Emacs , originally named EMACS (an acronym for "Editor MACroS"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as "the extensible, customizable, s ...
via web-mode.el
*
Notepad++
Notepad++ is a text and source code editor for use with Microsoft Windows. It supports tabbed editing, which allows working with multiple open files in a single window. The product's name comes from the C postfix increment operator.
Notepad++ ...
via the Notepad++ Twig Highlighter
*
Sublime Text
Sublime Text is a shareware text and source code editor available for Windows, macOS, and Linux. It natively supports many programming languages and markup languages. Users can customize it with themes and expand its functionality with plugi ...
via the Twig bundle
*
TextMate via the Twig bundle
*
Vim
Vim means enthusiasm and vigor. It may also refer to:
* Vim (cleaning product)
* Vim Comedy Company, a movie studio
* Vim Records
* Vimentin, a protein
* "Vim", a song by Machine Head on the album ''Through the Ashes of Empires''
* Vim (text ed ...
via the Jinja syntax plugin or the vim-twig plugin
*
Brackets via Brackets Twig
*
Visual Studio Code via the Twig extension
*
GTKSourceView via the Twig language definition
*
Coda
Coda or CODA may refer to:
Arts, entertainment, and media Films
* Movie coda, a post-credits scene
* ''Coda'' (1987 film), an Australian horror film about a serial killer, made for television
*''Coda'', a 2017 American experimental film from Na ...
via the Twig syntax mode
*
Coda 2 via the other Twig syntax mode
*
SubEthaEdit
SubEthaEdit is a collaborative real-time editor designed for Mac OS X. The name comes from the Sub-Etha communication network in ''The Hitchhiker's Guide to the Galaxy'' series.
History
SubEthaEdit was first released under the name Hydra in earl ...
via the Twig syntax mode
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
).
*
, to add comments in the templates. These comments aren't included in the rendered page.
*
, to execute statements, such as for-loops.
**
, to assign.
**
... : condition.
**
... : counter in a loop.
The apostrophe (') is the escape character.
To create an iterative array:
An associative array:
Operators precedence
The operators precedence is,
from the less to more priority:
Filters
The filters provide some treatments on an expression, when placed after it, separated by pipes. For example:
*
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:
* The CGI
environment variables, such as
.
Example
The example below demonstrates some basic features of Twig.
See also
*
Smarty
References
External links
*
Templating Engines in PHP Fabien Potencier, 2009
{{DEFAULTSORT:Twig (Template Engine)
Template engines
PHP software
Drupal