fbpx
Wikipedia

Twig (template engine)

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. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2.[5]

Twig
Original author(s)Armin Ronacher,[1] Fabien Potencier
Developer(s)Symfony SAS
Initial releaseOctober 12, 2009 (2009-10-12)
Stable release
3.7.1[2] / 28 August 2023; 42 days ago (28 August 2023)
Repository
  • github.com/twigphp/Twig
Written inPHP
Operating systemCross-platform
TypeTemplate engine
LicenseBSD License
Websitetwig.symfony.com

The same template language is used by the Nunjucks template engine, thus Nunjucks is also supported by the following tools.

Features Edit

  • Complex control flow
  • Automatic escaping
  • Template inheritance
  • Variable filters[6]
  • i18n support (gettext)
  • Macros
  • Fully extendable[3][7]

Twig is supported by the following integrated development environments:[3]

  • Eclipse 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)
  • IntelliJ IDEs, including WebStorm, via a plugin

And the text editors:

Syntax Edit

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 {{ parent() }}).
  • {# ... #}, to add comments in the templates. These comments aren't included in the rendered page.
  • {% ... %}, to execute statements, such as for-loops.
    • {% set foo = 'bar' %}, to assign.[8]
    • {% if i is defined and i == 1%} ... {% endif %}: condition.
    • {% for i in 0..10 %} ... {% endfor %}: counter in a loop.

The apostrophe (') is the escape character.

To create an iterative array:

{% set myArray = [1, 2] %} 

An associative array:

{% set myArray = {'key': 'value'} %} 

Operators precedence Edit

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

Operator Role
b-and Bitwise AND
b-xor Bitwise XOR
b-or Bitwise OR
or Or
and And
== Is equal?
!= Is different?
< Inferior
> Superior
>= Superior or equal
<= Inferior or equal
in Into
matches Corresponds
starts with Begins by
ends with Finishes by
.. Sequence (ex: 1..5)
+ Plus
- Less
~ Concatenation
* Multiplication
/ Division
// Division rounded to lower
% Modulo
is Test (ex: is defined or is not empty)
** Power
| Filter[6]
[] Array entry
. Attribute or method from an object (ex: country.name)

Filters Edit

The filters provide some treatments on an expression, when placed after it, separated by pipes. For example:[6]

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

  • 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: {{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
  • The CGI environment variables, such as {{ app.request.server.get('SERVER_NAME') }}.

Example Edit

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

See also Edit

References Edit

  1. ^ "mitsuhiko/twig". August 13, 2019 – via GitHub.
  2. ^ "Release 3.7.1". 28 August 2023. Retrieved 19 September 2023.
  3. ^ a b c d "Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine". twig.symfony.com.
  4. ^ "twigphp/Twig". July 1, 2020 – via GitHub.
  5. ^ . August 5, 2010. Archived from the original on 2010-08-05.
  6. ^ a b c "Filters - Documentation - Twig - The flexible, fast, and secure PHP template engine". twig.symfony.com.
  7. ^ "Extending Twig - Documentation - Twig - The flexible, fast, and secure PHP template engine". twig.symfony.com.
  8. ^ "set - Documentation - Twig - The flexible, fast, and secure PHP template engine". twig.symfony.com.

External links Edit

  • Twig official website
  • Templating Engines in PHP, Fabien Potencier, 2009

twig, template, engine, twig, template, engine, programming, language, syntax, originates, from, jinja, django, templates, open, source, product, licensed, under, license, maintained, fabien, potencier, initial, version, created, armin, ronacher, symfony, fram. 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 Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2 5 TwigOriginal author s Armin Ronacher 1 Fabien PotencierDeveloper s Symfony SASInitial releaseOctober 12 2009 2009 10 12 Stable release3 7 1 2 28 August 2023 42 days ago 28 August 2023 Repositorygithub wbr com wbr twigphp wbr TwigWritten inPHPOperating systemCross platformTypeTemplate engineLicenseBSD LicenseWebsitetwig wbr symfony wbr comThe same template language is used by the Nunjucks template engine thus Nunjucks is also supported by the following tools Contents 1 Features 2 Syntax 3 Operators precedence 3 1 Filters 3 2 Special variables 4 Example 5 See also 6 References 7 External linksFeatures EditComplex control flow Automatic escaping Template inheritance Variable filters 6 i18n support gettext Macros Fully extendable 3 7 Twig is supported by the following integrated development environments 3 Eclipse 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 IntelliJ IDEs including WebStorm via a pluginAnd the text editors Atom via the PHP twig for atom Emacs via web mode el Notepad via the Notepad Twig Highlighter Sublime Text via the Twig bundle TextMate via the Twig bundle Vim 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 via the Twig syntax mode Coda 2 via the other Twig syntax mode SubEthaEdit via the Twig syntax modeSyntax EditTwig 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 parent to add comments in the templates These comments aren t included in the rendered page to execute statements such as for loops span class cp span span class k set span span class nv foo span span class o span span class s1 bar span span class cp span to assign 8 span class cp span span class k if span span class nv i span span class k is span span class nf defined span span class k and span span class nv i span span class o span span class m 1 span span class cp span span class x span span class cp span span class k endif span span class cp span condition span class cp span span class k for span span class nv i span span class k in span span class m 0 10 span span class cp span span class x span span class cp span span class k endfor span span class cp span counter in a loop The apostrophe is the escape character To create an iterative array set myArray 1 2 An associative array set myArray key value Operators precedence EditThe operators precedence is 3 from the less to more priority Operator Roleb and Bitwise ANDb xor Bitwise XORb or Bitwise ORor Orand And Is equal Is different lt Inferior gt Superior gt Superior or equal lt Inferior or equalin Intomatches Correspondsstarts with Begins byends with Finishes by Sequence ex 1 5 Plus Less Concatenation Multiplication Division Division rounded to lower Modulois Test ex is defined or is not empty Power Filter 6 Array entry Attribute or method from an object ex country name Filters Edit The filters provide some treatments on an expression when placed after it separated by pipes For example 6 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 Edit 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 path app request attributes get route app request attributes get route params The CGI environment variables such as app request server get SERVER NAME Example EditThe example below demonstrates some basic features of Twig extends base html block navigation lt ul id navigation gt for item in navigation lt li gt lt a href item href gt if item level 2 amp nbsp amp nbsp endif item caption upper lt a gt lt li gt endfor lt ul gt endblock navigation See also Edit nbsp Free and open source software portalSmartyReferences Edit mitsuhiko twig August 13 2019 via GitHub Release 3 7 1 28 August 2023 Retrieved 19 September 2023 a b c d Twig for Template Designers Documentation Twig The flexible fast and secure PHP template engine twig symfony com twigphp Twig July 1 2020 via GitHub Symfony2 Documentation Documentation August 5 2010 Archived from the original on 2010 08 05 a b c Filters Documentation Twig The flexible fast and secure PHP template engine twig symfony com Extending Twig Documentation Twig The flexible fast and secure PHP template engine twig symfony com set Documentation Twig The flexible fast and secure PHP template engine twig symfony com External links EditTwig official website Templating Engines in PHP Fabien Potencier 2009 Retrieved from https en wikipedia org w index php title Twig template engine amp oldid 1154922171, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.