1. {%… %}, {{… }}, {#… #}

{% for item in list %}
    {{ item }}
{% endfor %}
Copy the code

{{… }} to print the result of a template expression {{item}}

{#… {{item}} #}

2. The cycle

{% for key,item in list %} <div>{{ key }}---{{ item['user'] }}</div> <div>{{ loop.index }}</div> {% endfor %} {user:'123',num:'1'}, we can use item['user'] or item.user inside the loop: Index number of loops (starting from 1) loop.index0 number of loops (starting from 0) loop.revIndex Number of loops remaining (min. 1) loop.revIndex0 Number of loops remaining (min. 0) loop.first Last Returns true loop.length The total number of loops loop.parent The array loop to be looped must have an end {% endfor %}Copy the code

3. The judge

{% for key,item in list %} <div>{{ key }}---{{ item['user'] }}</div> {% endfor %} {% elseif list|length > 0 %} <div> Multiple judgment conditions < / div >} else {% % < div > multiple judgment conditions < / div > {% endif %} | - on behalf of the filter, {% endif %} {% if list is null %} <div> {% endif %} {% if list is not null </div Defined %} < / div > < div > determine whether definition endif {% %} {% if the list or the list | length > 0%} < div > or use the or < / div > {% endif %} {% the if List and list | length > 0%} < div > and out and use the < / div > {% endif %} loop judge {% for the key, the item in the list. If the item the user = = '456' %} The <div>{{key}}-- {{item['user']}}</div> {% endfor %} loop can also be combined with if judgment, so that the output will be 1-- 456 and the other two arrays will not be renderedCopy the code

4. Automatic escape

{% endautoescape %} {% autoescape false %} {% autoescape false %} Do not escape {% endautoescape %} Twig 1.8 above {% autoescape %} This content is automatically escaped with HTML escape strategy {% endautoescape %} {% autoescape 'HTML' %} {% endautoescape %} {% autoescape 'js' %} {% endautoescape %} {% autoescape false {% endautoescape %}Copy the code

5. | filters

Default: Provides a default value if the modified data does not exist or is empty. Such as{{' |default(' default ')}}Length: returns the number of elements in the array or the length of the string. Lower: uppercases all the letters of the string. Upper: uppercases all the letters of the string. Capitalize the first letter of each word in a string split: Splits the string into arraysCopy the code

6. Several functions used

Even: whether the value is even, for example, {% if I is even %} odd: whether the value is odd, for example, {% if I is odd %} empty: whether the value is empty, for example, {% if I is empty %} null: Null or not, for example {% if I is null %} defined: Specifies or not defined, for example {% if I is defined %}Copy the code