Twig's tags learning (Chinese) Part 1_PHP tutorial
Jul 13, 2016 pm 05:48 PM
Twig is a simple yet powerful template. I’m learning SF so take a look at her.
The source of this article is 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 3 types in html
{{...}} directly outputs the variables
{#...#} comment tag
{%...%} command tags are what we need to learn
for tag
This is the simplest, it is a loop.
Array-based loop
<h1>Members</h1>
<ul>
{% for user in users %}
{% endfor %}
</ul>
<h1>Members</h1>
<ul>
{% for user in users %}
{% endfor %}
</ul>
For loops based on numbers, special attention should be paid to the fact that 0-10, which is 11 numbers, will be output here.
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
Letter-based loop
* {{ letter }}
{% endfor %}
{% for letter in 'a'..'z' %}
* {{ letter }}
{% endfor %}
Variables inside the loop body
loop.length, loop.revindex, loop.revindex0, loop.last These values are only valid when the loop is a PHP array or a class that implements the Countable interface.
Add a condition
Unlike PHP, break and continue statements are not supported inside loops. You can only skip some loops through filters, like this
<ul>
{% for user in users if user.active %}
{% endfor %}
</ul>
<ul>
{% for user in users if user.active %}
{% endfor %}
</ul>
If users is an empty array, no user found will be output.
<ul>
{% else %}
{% endfor %}
</ul>
<ul>
{% for user in users %}
{% else %}
{% endfor %}
</ul>
Loop by keys
<h1>Members</h1>
{% for key in users|keys %}
{% endfor %}</ul>
<h1>Members</h1>
<ul>
{% for key in users|keys %}
{% endfor %}
</ul>
Loop by keys, values
<h1>Members</h1>
<ul>
{% for key, user in users %}
</ul>
<h1>Members</h1>
{% for key, user in users %}
{% endfor %}
</ul>
if tag
Needless to say, just look at the example {% if users %}
<ul>
{% for user in users %}
{% 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 (macro tag) is similar to functions in other languages and is often used to fill in html tags. The following is an example to render <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与函数的不同之处在于:
1、参数的默认值是通过macro块内部的 default过滤器来定义的。
2、参数总是可选的。
另外,就跟php函数一样,macro内部是无法使用外部的变量的。但你可以传递一个特殊变量_context作为参数来获取整个内容。
macro可以被定义在任何的模板内,但在你使用之前需要使用 imported
{% 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>如果你要在定义macro的模板里使用,就不需要imported 可以使用特殊变量_self
<p>{{ _self.input('username') }}</p>
<p>{{ _self.input('username') }}</p>
如果你要定义一个macro里 包含另一个macro,并且两个macro在同一个文件里,可以使用特殊变量_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 %}
如果两个macro在不同的文件里,你需要使用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
Just apply a filter to 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 %}
Excerpted from jiaochangyun’s column

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to set Chinese in Call of Duty: Warzone mobile game

Setting up Chinese with VSCode: The Complete Guide

Tips for solving Chinese garbled characters when writing txt files with PHP

Let's learn how to input the root number in Word together

Effects of C++ template specialization on function overloading and overriding

Practical Tips: How to use the trim function in PHP to process Chinese spaces

How to change C++ software to Chinese
