Friday 5 May 2017

Twig in Drupal 8.2

By
Vikas,


Twig is flexible, fast, and secure template engine for PHP, which is designer and developer friendly by sticking to PHP's principles and adding functionality useful for template environments.




Followings are the key-features of Twig :-
• Fast: it compiles templates down to plain optimized PHP code. Twig reduced overhead to the very minimum as compared to regular PHP code.

• Secure: it has a sandbox mode to evaluate untrusted template code, which allows it to be used as a template language for applications where users may modify the template design.

• Flexible: it is powered by a flexible lexer and parser, which allows the developer to define their own custom tags and filters.
Twig is used by many Open-Source projects like Symfony, Drupal8, phpBB and many frameworks have support for it as well like Slim, Laravel, Codeigniter and Kohana etc.
Twig template is simply as a text file, which can generate by any text-based format like HTML, XML, CSV, LaTeX, etc. It doesn't have a specific extension.
It contains variables or expressions, which get replaced with values when the template is evaluated, and tags, which control the logic of the template.

For Example:
<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
        {% endfor %}
        </ul>
        <h1>My Webpage</h1>
        {{ a_variable }}
    </body>
</html>
There are two kinds of delimiters: {% ... %} and {{ ... }}.
Variables
The application passes variables to the templates for manipulation in the template. Variables may have attributes or elements you can access. The visual representation of a variable depends heavily on the application providing it.
You can use a dot (.) to access attributes of a variable or the called "subscript" syntax:
For example:
{{ foo.bar }}
{{ foo['bar'] }}
Global Variables
The following variables are always available in templates:
• _self:  which is used for references the current template;
• _context: which is used for references the current context;
• _charset: which is used for references the current charset.
Setting Variables¶
By using the set tag, you can assign values to variables inside code blocks:
For example:
1. {% set foo = 'foo' %}
2. {% set foo = [1, 2] %}
3. {% set foo = {'foo': 'bar'} %}
Filters
Variables can be modified by using the filters. Pipe symbol (|) is used to separate the filter from the variable and may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.
For example:
1. {% filter upper %}
2.    This text becomes uppercase
3. {% endfilter %}
Functions
By calling the Functions, we can generate the content. Functions are called by their name followed by parentheses (()) and may have arguments.
For instance, the range function returns a list containing an arithmetic progression of integers:

1. {% for i in range(0, 3) %}
 2. {{ i }},
3. {% endfor %}
Control Structure
It refers to all those things that control the flow of a program i.e. conditionals statement (if/elseif/else, for loops). Control structures appear inside {% ... %} blocks.
For example, to display a list of users provided in a variable called user, use the for tag:

1. <h1>Members</h1>
2. <ul>
3.    {% for user in users %}
4.        <li>{{ user.username|e }}</li>
5.    {% endfor %}
6. </ul>
Comments
To comment-out part of a line in a template, use the comment syntax {# ... #}. Comments in twig is useful to template designers or yourself for debugging or to add information:

For example:
1. {# note: comment section in the template because we no longer use this
2.    {% for user in users %}
3.         ...
4.     {% endfor %}
5. #}

No comments:

Post a Comment