One of Twig's tags Learning (Chinese)

Source: Internet
Author: User

Twig is a simple and powerful template, because she is learning sf.

This article source http://twig.sensiolabs.org/doc/tags/index.html

 


Currently supported tags include

For if macro filter set extends block include import from use spaceless autoescape raw flush do

Twig is divided into three types in html

{...} Directly outputs the variables.


{#... #} Annotation label


The {%... %} command label is what we want to learn.


For tag
This is a loop.

Array-based Loop


<H1> Members <Ul>
{% For user in users %}
<Li >{{ user. username | e }}</li>
{% Endfor %}
</Ul>
<H1> Members <Ul>
{% For user in users %}
<Li >{{ user. username | e }}</li>
{% Endfor %}
</Ul>
A number-based loop. Note that here 0-10 is output, that is, 11 digits.

 

{% For I in 0 .. 10%}
* {I }}
{% Endfor %}
{% For I in 0 .. 10%}
* {I }}
{% Endfor %}

Letter-based Loop


{% For letter in 'A' .. 'Z' %}
* {Letter }}
{% Endfor %}
{% For letter in 'A' .. 'Z' %}
* {Letter }}
{% Endfor %}
Variable inside the loop body

Variable name Description
Loop. index Number of cycles (starting from 1)
Loop. index0 Number of cycles (starting from 0)
Loop. revindex Number of remaining cycles (minimum value: 1)
Loop. revindex0 Number of remaining cycles (minimum value: 0)
Loop. first Returns true in the first loop.
Loop. last Returns true for the last loop.
Loop. length Total number of cycles
Loop. parent Cyclic Array
 

Loop. length, loop. revindex, loop. revindex0, and loop. last values are valid only when the php array or the Countable interface class is cyclically implemented.


Add a condition
Unlike PHP, the break and continue statements are not supported in the loop. You can only skip some loops through the filter, just like this


<Ul>
{% For user in users if user. active %}
<Li >{{ user. username | e }}</li>
{% Endfor %}
</Ul>
<Ul>
{% For user in users if user. active %}
<Li >{{ user. username | e }}</li>
{% Endfor %}
</Ul>


Else Branch
If users is an empty array, no user found is output.

<Ul>
{% For user in users %}
<Li >{{ user. username }}</li>
{% Else %}
<Li> <em> no user found </em> </li>
{% Endfor %}
</Ul>
<Ul>
{% For user in users %}
<Li >{{ user. username }}</li>
{% Else %}
<Li> <em> no user found </em> </li>
{% Endfor %}
</Ul>

 

Cycle by keys

<H1> Members <Ul>
{% For key in users | keys %}
<Li >{{ key }}</li>
{% Endfor %}
</Ul>
<H1> Members <Ul>
{% For key in users | keys %}
<Li >{{ key }}</li>
{% Endfor %}
</Ul>


Cycle by keys and values


<H1> Members <Ul>
{% For key, user in users %}
<Li >{{ key }:{{ user. username | e }}</li>
{% Endfor %}
</Ul>
<H1> Members <Ul>
{% For key, user in users %}
<Li >{{ key }:{{ user. username | e }}</li>
{% Endfor %}
</Ul>

 

If tag
For more information, see the example {% if users %}
<Ul>
{% For user in users %}
<Li >{{ user. username | e }}</li>
{% Endfor %}
</Ul>
{% Endif %}
 
{% If kenny. sick %}
Kenny is sick.
{% Elseif kenny. dead %}
You killed Kenny! You bastard !!!
{% Else %}
Kenny looks okay --- so far
{% Endif %}
{% If users %}
<Ul>
{% For user in users %}
<Li >{{ user. username | e }}</li>
{% Endfor %}
</Ul>
{% Endif %}

{% If kenny. sick %}
Kenny is sick.
{% Elseif kenny. dead %}
You killed Kenny! You bastard !!!
{% Else %}
Kenny looks okay --- so far
{% Endif %}

 

Macro label
Macro (macro tag) is similar to functions in other languages and is often used to fill html tags. The following is an example for rendering <input>


{% Macro input (name, value, type, size) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}
{% Macro input (name, value, type, size) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}
Macro differs from functions in the following ways:

1. The default value of the parameter is defined by the default filter in the macro block.

2. parameters are always optional.

In addition, like php functions, macro cannot use external variables. However, you can pass a special Variable _ context as a parameter to obtain the entire content.

Macro can be defined in any template, but you need to use imported before using it.


{% Import "forms.html" as forms %}
{% Import "forms.html" as forms % }.


<P >{{ forms. input ('username') }}</p>
<P >{{ forms. input ('Password', null, 'Password') }}</p>
<P >{{ forms. input ('username') }}</p>
<P> {forms. input ('Password', null, 'Password') }}</p> if you want to use it in the template defining macro, you do not need to use the special Variable _ self for imported.


<P >{{_ self. input ('username') }}</p>
<P >{{_ self. input ('username') }}</p>
If you want to define a macro that contains another macro and two macro are in the same file, you can use the special Variable _ self


{% Macro input (name, value, type, size) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}
 
{% Macro wrapped_input (name, value, type, size) %}
<Div class = "field">
{_ Self. input (name, value, type, size )}}
</Div>
{% Endmacro %}
{% Macro input (name, value, type, size) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}

{% Macro wrapped_input (name, value, type, size) %}
<Div class = "field">
{_ Self. input (name, value, type, size )}}
</Div>
{% Endmacro %}
If two macro files are in different files, you need to use import


{# Forms.html #}
 
{% Macro input (name, value, type, size) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}
 
{# Shortcuts.html #}
 
{% Macro wrapped_input (name, value, type, size) %}
{% Import "forms.html" as forms %}
<Div class = "field">
{Forms. input (name, value, type, size )}}
</Div>
{% Endmacro %}
{# Forms.html #}

{% Macro input (name, value, type, size) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}

{# Shortcuts.html #}

{% Macro wrapped_input (name, value, type, size) %}
{% Import "forms.html" as forms %}
<Div class = "field">
{Forms. input (name, value, type, size )}}
</Div>
{% Endmacro %}


Filter tag
The filter is used for the entire block.


{% Filter upper %}
This text becomes uppercase
{% Endfilter %}
{% Filter upper %}
This text becomes uppercase
{% Endfilter %}
{% Filter lower | escape %}
<Strong> some text </strong>
{% Endfilter %}

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.