Twig (template engine)

Last updated
Twig
Original author(s) Armin Ronacher, [1] Fabien Potencier
Developer(s) Symfony SAS
Initial releaseOctober 12, 2009 (2009-10-12)
Stable release
3.11.0 [2] / 8 August 2024;22 days ago (8 August 2024)
Repository
Written in PHP
Operating system Cross-platform
Type Template engine
License BSD License
Website twig.symfony.com

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]

Contents

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

Features

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

And the text editors:

Syntax

Twig defines three kinds of delimiters:

The apostrophe (') is the escape character.

To create an iterative array:

{%setmyArray=[1,2]%}

An associative array:

{%setmyArray={'key':'value'}%}

Operators precedence

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

OperatorRole
b-and Bitwise AND
b-xor Bitwise XOR
b-or Bitwise OR
orOr
andAnd
==Is equal?
 !=Is different?
<Inferior
>Superior
>=Superior or equal
<=Inferior or equal
inInto
matchesCorresponds
starts withBegins by
ends withFinishes by
..Sequence (ex: 1..5)
+Plus
-Less
~Concatenation
*Multiplication
/Division
//Division rounded to lower
 %Modulo
isTest (ex: is defined or is not empty)
**Power
|Filter [6]
[]Array entry
.Attribute or method from an object (ex: country.name)

Filters

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

Special variables

Example

The example below demonstrates some basic features of Twig.

{%extends"base.html"%}{%blocknavigation%}<ulid="navigation">{%foriteminnavigation%}<li><ahref="{{item.href}}">{%ifitem.level==2%}&nbsp;&nbsp;{%endif%}{{item.caption|upper}}</a></li>{%endfor%}</ul>{%endblocknavigation%}

See also

Related Research Articles

<span class="mw-page-title-main">PHP</span> Scripting language created in 1994

PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. PHP was originally an abbreviation of Personal Home Page, but it now stands for the recursive acronym PHP: Hypertext Preprocessor.

4D is a relational database management system and integrated development environment developed by Laurent Ribardière. 4D was created in 1984 and had a slightly delayed public release for Macintosh in 1987 with its own programming language.

This article provides basic comparisons for notable text editors. More feature details for text editors are available from the Category of text editor features and from the individual products' articles. This article may not be up-to-date or necessarily all-inclusive.

The following tables compare general and technical information for many wiki software packages.

<span class="mw-page-title-main">Django (web framework)</span> Python web framework

Django is a free and open-source, Python-based web framework that runs on a web server. It follows the model–template–views (MTV) architectural pattern. It is maintained by the Django Software Foundation (DSF), an independent organization established in the US as a 501(c)(3) non-profit.

Open Power Template is a web template engine written in PHP 5. A common strategy in designing web application is separation of the application logic from the presentation. OPT is a tool for implementing such separation. The presentation layer is represented by templates, text files with HTML code and extra instructions controlling the data substitution.

<span class="mw-page-title-main">Symfony</span> PHP web application framework for MVC applications

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.

<span class="mw-page-title-main">Jinja (template engine)</span> Web template engine for Python

Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code.

A personal wiki is wiki software that allows individual users to organize information on their desktop or mobile computing devices in a manner similar to community wikis, but without collaborative software or multiple users.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

Haml is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner. Similar to other template systems like eRuby, Haml also embeds some code that gets executed during runtime and generates HTML code in order to provide some dynamic content. In order to run Haml code, files need to have a .haml extension. These files are similar to .erb or .eRuby files, which also help embed Ruby code while developing a web application.

Conditional comments are conditional statements interpreted by Microsoft Internet Explorer versions 5 through 9 in HTML source code. They can be used to provide and hide code to and from these versions of Internet Explorer. Conditional comments are not supported in Internet Explorer 10 and 11.

Flow is a free and open source web application framework written in PHP. The first final version was released on October 20, 2011. It was primarily designed as a basis for the content management system Neos, but can also be used independently. It is generally suitable for PHP development of mid- or large-scaled web applications.

The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.

XPath is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values from the content of an XML document. Support for XPath exists in applications that support XML, such as web browsers, and many programming languages.

Emmet is a set of plug-ins for text editors that allows for high-speed coding and editing in HTML, XML, XSLT, and other structured code formats via content assist. The project was started by Vadim Makeev in 2008 and continues to be actively developed by Sergey Chikuyonok and Emmet users. Since 2015, Mikael Geletsyan is responsible for UX at Emmet. The tools have been incorporated into several popular text editors, as well as some plug-ins developed by the Emmet team and others implemented independently. However, Emmet is primarily independent from any text editor, as the engine works directly with text rather than with any particular software.

Pimcore is an open-source enterprise PHP software platform for product information management (PIM), master data management (MDM), customer data management (CDP), digital asset management (DAM), content management (CMS), and digital commerce.

Mustache is a web template system. It is described as a logic-less system because it lacks any explicit control flow statements, like if and else conditionals or for loops; however, both looping and conditional evaluation can be achieved using section tags processing lists and anonymous functions (lambdas). It is named "Mustache" because of heavy use of braces, { }, that resemble a sideways moustache. Mustache is used mainly for mobile and web applications.

BitFunnel is the search engine indexing algorithm and a set of components used in the Bing search engine, which were made open source in 2016. BitFunnel uses bit-sliced signatures instead of an inverted index in an attempt to reduce operations cost.

References

  1. "mitsuhiko/twig". GitHub. August 13, 2019.
  2. "Release 3.11.0". 8 August 2024. Retrieved 27 August 2024.
  3. 1 2 3 4 "Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  4. "twigphp/Twig". GitHub. July 1, 2020.
  5. "Symfony2 Documentation — Documentation". Twig. August 5, 2010. Archived from the original on 2010-08-05.
  6. 1 2 3 "Filters - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  7. "Extending Twig - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.
  8. "set - Documentation - Twig - The flexible, fast, and secure PHP template engine". Twig.