Home Backend Development PHP Tutorial Twig's tags learning (Chinese) Part 1_PHP tutorial

Twig's tags learning (Chinese) Part 1_PHP tutorial

Jul 13, 2016 pm 05:48 PM
twig Chinese exist study template of Simple

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

{% for letter in 'a'..'z' %}

* {{ 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>

else branch

If users is an empty array, no user found will be output.

<ul>

{% for user in users %}

                                                                                                                {% else %}
                                                                                                      {% endfor %}
</ul>
<ul>
{% for user in users %}
                                                                                                          {% else %}
                                                                                {% endfor %}
</ul>



Loop by keys

<h1>Members</h1>

<ul>

{% 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 %}

                                                                                                                     {% endfor %}

</ul>
<h1>Members</h1>

<ul>

{% 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 tag

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478461.htmlTechArticleTwig is a simple and powerful template, because I am learning SF, so take a look at her. Source of this article http://twig.sensiolabs.org/doc/tags/index.html Currently supported tags include for if macro filter...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set Chinese in Call of Duty: Warzone mobile game How to set Chinese in Call of Duty: Warzone mobile game Mar 22, 2024 am 08:41 AM

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

Setting up Chinese with VSCode: The Complete Guide Setting up Chinese with VSCode: The Complete Guide Mar 25, 2024 am 11:18 AM

Setting up Chinese with VSCode: The Complete Guide

Tips for solving Chinese garbled characters when writing txt files with PHP Tips for solving Chinese garbled characters when writing txt files with PHP Mar 27, 2024 pm 01:18 PM

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

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

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

How to add PPT mask How to add PPT mask Mar 20, 2024 pm 12:28 PM

How to add PPT mask

Effects of C++ template specialization on function overloading and overriding Effects of C++ template specialization on function overloading and overriding Apr 20, 2024 am 09:09 AM

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

Practical Tips: How to use the trim function in PHP to process Chinese spaces Practical Tips: How to use the trim function in PHP to process Chinese spaces Mar 27, 2024 am 11:27 AM

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

How to change C++ software to Chinese How to change C++ software to Chinese Mar 21, 2024 pm 03:07 PM

How to change C++ software to Chinese

See all articles