Twig (template engine)
Twig | |
---|---|
Basic data
|
|
Maintainer | Armin Ronacher , Fabien Potencier |
developer | SensioLabs |
Publishing year | 2009 |
Current version |
3.0.5 ( August 5, 2020 ) |
operating system | Platform independent |
programming language | PHP |
category | Template engine |
License | BSD license |
twig.symfony.com |
Twig is a template engine for the PHP programming language . The syntax was influenced by the Jinja template engine and the Django framework . Twig is free software , under the BSD license , and is maintained by Fabien Potencier. The first version was developed by Armin Ronacher . The PHP framework Symfony2 uses Twig as the standard for the template engine.
Functions
- Support flow of control
- automatic masking
- Inheritance of templates
- variable filters
- Support for internationalization via gettext
- Macros
- Expandability
Twig is supported by the following Integrated Development Environments:
- Eclipse
- Komodo
- NetBeans
- PhpStorm (native)
- Visual studio
The templating engine is also supported by the following editors:
syntax
Twig works with three types of identifiers:
-
{{ ... }}
{{ parent() }}
to output the content of a variable or the result of an expression (e.g. to include an inherited Twig template ). -
{# ... #}
, for comments that are not processed further -
{% ... %}
, for commands and control structures through e.g. B. loops or branches-
{% set foo = 'bar' %}
, for the value assignment. -
{% if i is defined and i == 1%} ... {% endif %}
: Conditional statement and branch . -
{% for i in 0..10 %} ... {% endfor %}
: Grinding
-
The apostrophe (') is the escape sequence .
So an iterative array can be created in the following way:
{% set meinArray = [1, 2] %}
The same applies to associative data fields :
{% set meinArray = {'key': 'value'} %}
Operators and precedence
The operators and their precedence are listed in the following table in ascending order of priority:
operator | function |
---|---|
tape | Conjunction (logical) |
b-xor | Contravalence (logical) |
boron | Disjunction (logical) |
or | Disjunction |
other | conjunction |
== | equality |
! = | inequality |
< | Less than |
> | Greater than |
> = | Greater or equal |
<= | Less than or equal to |
in | within |
matches | corresponds |
starts with | begins with |
ends with | ends with |
.. | Sequence (eg. B .: 1..5 )
|
+ | plus |
- | minus |
~ | Concatenation |
* | multiplication |
/ | division |
// | Division (rounded down) |
% | Modulo |
is | Test (ex: ist definiert or ist nicht leer )
|
** | power |
| | filter |
[] | Data field |
. | Attribute or method of an object (eg. B .: land.name )
|
filter
Filters enable the further processing of an expression before the output and are introduced by a pipe after the expression. Possible filters are, for example:
-
capitalize
: changes the first character of a string to an upper case letter. -
upper
: changes all characters in a string to uppercase. -
first
: shows the first line of a data field. -
length
: returns the size of the variable value.
Special variables
-
loop
contains information about the current iteration within a loop. So returnsloop.index
the number of iterations already executed. - Global variables begin with underscores. For example:
- _route (URL part behind the domain)
- _self (own file name)
- The path to the page will be displayed in the following way:
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
- The CGI environment variables , e.g. B.
{{ app.request.server.get('SERVER_NAME') }}
.
example
The following example shows the basic use 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
- Smarty
- Twital , a template engine that compiles its sources in Twig templates and thus offers additional functions such as context-based masking, attribute expressions and greater readability.
Web links
- Official website
- Fabien Potencier: Templating Engines in PHP . October 7, 2009. Retrieved April 6, 2011.
- Fabien Potencier: Templating Engines in PHP (Перевод: Шаблонизаторы в PHP) ( ru ) November 20, 2009. Accessed April 6, 2011.
Individual evidence
- ↑ Original repository
- ↑ Release 3.0.5 . August 5, 2020 (accessed August 16, 2020).
- ↑ a b Twig documentation for template designers
- ↑ Twig repository on GitHub
- ↑ Symfony2 Documentation ( Memento of the original from August 5, 2010 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ a b c twig.symfony.com
- ↑ Extending Twig
- ↑ twig.symfony.com
- ↑ twig.symfony.com
- ↑ twig.symfony.com