Twig (template engine)

from Wikipedia, the free encyclopedia
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:

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

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 definiertor 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

  • loopcontains information about the current iteration within a loop. So returns loop.indexthe 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 %}&nbsp;&nbsp;{% endif %}
                {{ item.caption|upper }}
            </a>
        </li>
    {% endfor %}
    </ul>
{% endblock navigation %}

See also

Portal: Free Software  - Overview of Wikipedia content on the subject of Free Software
  • 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

Individual evidence

  1. Original repository
  2. Release 3.0.5 . August 5, 2020 (accessed August 16, 2020).
  3. a b Twig documentation for template designers
  4. Twig repository on GitHub
  5. 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. @1@ 2Template: Webachiv / IABot / docs.symfony-reloaded.org
  6. a b c twig.symfony.com
  7. Extending Twig
  8. twig.symfony.com
  9. twig.symfony.com
  10. twig.symfony.com