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