A | B | C | D | E | F | G | H | CH | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Original author(s) | Armin Ronacher,[1] Fabien Potencier |
---|---|
Developer(s) | Symfony SAS |
Initial release | October 12, 2009 |
Stable release | 3.10.3[2]
/ 16 May 2024 |
Repository | |
Written in | PHP |
Operating system | Cross-platform |
Type | Template engine |
License | BSD License |
Website | twig |
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]
The same template language is used by the Nunjucks template engine, thus Nunjucks is also supported by the following tools.
Features
- 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:
- 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 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{{ 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 = %}
An associative array:
{% set myArray = {'key': 'value'} %}
Operators precedence
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
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
loop
contains the current loop information. For exampleloop.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
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 %} {% endif %}
{{ item.caption|upper }}
</a>
</li>
{% endfor %}
</ul>
{% endblock navigation %}
See also
References
- ^ "mitsuhiko/twig". August 13, 2019 – via GitHub.
- ^ "Release 3.10.3". 16 May 2024. Retrieved 25 May 2024.
- ^ 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
- Twig official website
- Fabien Potencier (2009-10-07). "Templating Engines in PHP". Retrieved 2023-10-27.
Text je dostupný za podmienok Creative Commons Attribution/Share-Alike License 3.0 Unported; prípadne za ďalších podmienok. Podrobnejšie informácie nájdete na stránke Podmienky použitia.
Antropológia
Aplikované vedy
Bibliometria
Dejiny vedy
Encyklopédie
Filozofia vedy
Forenzné vedy
Humanitné vedy
Knižničná veda
Kryogenika
Kryptológia
Kulturológia
Literárna veda
Medzidisciplinárne oblasti
Metódy kvantitatívnej analýzy
Metavedy
Metodika
Text je dostupný za podmienok Creative
Commons Attribution/Share-Alike License 3.0 Unported; prípadne za ďalších
podmienok.
Podrobnejšie informácie nájdete na stránke Podmienky
použitia.
www.astronomia.sk | www.biologia.sk | www.botanika.sk | www.dejiny.sk | www.economy.sk | www.elektrotechnika.sk | www.estetika.sk | www.farmakologia.sk | www.filozofia.sk | Fyzika | www.futurologia.sk | www.genetika.sk | www.chemia.sk | www.lingvistika.sk | www.politologia.sk | www.psychologia.sk | www.sexuologia.sk | www.sociologia.sk | www.veda.sk I www.zoologia.sk