TWIG function learning

Source: Internet
Author: User
Currently, the built-in twig functions include attribute, block, constant, cycle, dump, parent, random, and range. In fact, some of them have been seen in the learning of tags. The addition of attribute function 1.2 is equivalent to A. operator. {Attribute (o...

Currently, the built-in functions of twig include:

Attribute, block, constant, cycle, dump, parent, random, range.


In fact, some functions have been seen in the learning of tags.

 


Attribute function
New version 1.2
It is equivalent to A. operator.
{Attribute (object, method )}}
{Attribute (object, method, arguments )}}
{Attribute (array, item )}}
{Attribute (object, method )}}
{Attribute (object, method, arguments )}}
{Attribute (array, item )}}

 

 

Block function
Output the block content.
{% Block title % }{% endblock %} 
 
{Block ('title ')}}
 
{% Block body %} {% endblock %}
{% Block title % }{% endblock %}

{Block ('title ')}}

{% Block body %} {% endblock %}

 

 

Constant function
Read constant {some_date | date (constant ('date _ W3C '))}}
{Constant ('namespace \ Classname: CONSTANT_NAME ')}}
{Some_date | date (constant ('date _ W3C '))}}
{Constant ('namespace \ Classname: CONSTANT_NAME ')}}

 


Cycle function
Cyclically output the content of the array,
{% Set fruits = ['apple', 'Orange ', 'citrus'] %}
 
{% For I in 0 .. 10%}
{Cycle (fruits, I )}}
{% Endfor %}
{% Set fruits = ['apple', 'Orange ', 'citrus'] %}

{% For I in 0 .. 10%}
{Cycle (fruits, I )}}
{% Endfor %}

 

 

Dump function
New version 1.5
The var_dump function of php is used to print the variable,
In addition, the debug mode is not enabled for twig by default. you need to enable the debug mode first.
$ Twig = new Twig_Environment ($ loader, $ config );
$ Twig-> addExtension (new Twig_Extension_Debug ());
$ Twig = new Twig_Environment ($ loader, $ config );
$ Twig-> addExtension (new Twig_Extension_Debug ());

You can pass one or more variables. if you do not pass a variable, it prints all the variables.
{Dump (user, categories )}}
{Dump ()}}
{Dump (user, categories )}}
{Dump ()}}

 

 

Parent function
Obtain the content of the parent block, which is particularly useful when you are preparing to add, not overwrite
{% Extends "base.html" %}
 
{% Block sidebar %}
Table Of Contents
...
{Parent ()}}
{% Endblock %}
{% Extends "base.html" %}

{% Block sidebar %}
Table Of Contents
...
{Parent ()}}
{% Endblock %}

 

 

Random function
New in version 1.5, returns a random value from an array.
{Random (['apple', 'Orange ', 'citrus'])}
{Random (['apple', 'Orange ', 'citrus'])}

 

 

Range function
Returns an array of numbers starting from the first parameter and ending with the second parameter (including the second parameter). The third parameter is the step size (which can be omitted ). Same as 0 .. 10
{% For I in range (0, 3) %}
{I }},
{% Endfor %}
 
{# Returns 0, 1, 2, 3 #}
{% For I in range (0, 3) %}
{I }},
{% Endfor %}

{# Returns 0, 1, 2, 3 #}


I learned a lot .. Continue .. Move to a new page...

From the column jiaochangyun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.