Branch is the template engine for the PHP programming language. The syntax comes from Jinja and Django templates. It is an open source product licensed under the BSD License and managed by Fabien Potencier. Early versions were made by Armin Ronacher. The Symfony2 PHP framework comes with bundle support for Twig as its default template engine.
Video Twig (template engine)
Features
- complex control flow
- auto out
- legacy template
- variable filters
- support i18n (gettext)
- macro
- fully renewable
Twig is supported by the following integrated development environment:
- Eclipse via Twig plugin
- Komodo and Komodo Edit via the spotlight mode/spotlight syntax Twig
- NetBeans via the Twig syntax plugin (up to 7.1, original at 7.2)
- PhpStorm (native to 2.1)
And a text editor:
- Atom through PHP-twig for atom
- emacs via web-mode.el
- Notepad via Notepad Twig Highlighter
- Sublime Text via Twig bundle
- TextMate via Twig bundle
- vim via Jinja syntax plugin or vim-twig plugin
- Visual Studio Code via Twig extension
- GTKSourceView via twitter definition
- Coda via Twang syntax mode
- Coda 2 via other Twig syntax mode
- SubEthaEdit via Twig syntax mode
Maps Twig (template engine)
Syntax
Twig defines three types of delimiter:
-
{{...}}
, to print variable content or expression evaluation results (for example: Twig template inherited with{{parent ()}}
). -
{#... #}
, to add comments in the template. This comment is not included in the displayed page. -
{%... Ã,%}
, to execute statements, such as for-loops.-
{% set foo = 'bar' %}
, to set. -
{% if i is defined and i == 1 %} ... {% endif %}
: condition. -
{% for i in 0..10 %} ... {% endfor %}
: count in a loop.
-
The apostrophe (') is the character of the runaway.
To create an iterative array:
Associative Array:
Preferred operator
Operator priority is, of fewer priorities:
Filters
The filter provides some treatment on the expression, when placed thereafter, separated by a pipe. As an example:
-
capitalization
: change the first letter of the string in capital. -
upper
: change the whole string in capital. -
first
: shows the first line of an array. -
length
: returns the size of the variable.
Custom variables
-
loop
contains the current loop information. For exampleloop.index
corresponds to the number of iterations that have already occurred. - Global variables begin with underscores. As an example:
- _route (the portion of the URL located after the domain)
- _self (current filename)
- So, to route the page:
{{path (app.request.attributes.get ('_ route'), app.request.attributes.get ('_ route_params') )}}
- CGI environment variables, such as
{{app.request.server.get ('SERVER_NAME')}}
.
Example
The example below shows some basic features of Twig.
See also
- Smarty
- Twital, a template engine that compiles its source into Twig template, adds several features as conscious context-browsing, attribute expression, and makes the templates more readable.
- Using Twig in Drupal 8 cores
References
External links
- Twig's official website
- Potencier, Fabien (October 7, 2009). "Templating Engines in PHP" . Retrieved April 6 2011 .
- Potencier, Fabien (November 20, 2009). "Templating Engine in PHP (???????: ????????????????????????????????????????????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????? Got 6 April 2011 .
Source of the article : Wikipedia