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

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

WBOY
Release: 2016-07-13 17:48:01
Original
1102 people have browsed it

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


Members



    {% for user in users %}
                                                                                                           {% endfor %}

Members



    {% for user in users %}
                                                                                                            {% endfor %}

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



    {% for user in users if user.active %}
                                                                                                           {% endfor %}


    {% for user in users if user.active %}
                                                                                                            {% endfor %}


else branch

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

    {% for user in users %}

                                                                                                                    {% else %}
                                                                                                          {% endfor %}



    {% for user in users %}
                                                                                                              {% else %}
                                                                                    {% endfor %}




Loop by keys

Members

    {% for key in users|keys %}

                                                                                                              {% endfor %}

Members


    {% for key in users|keys %}
                                                                                                            {% endfor %}



Loop by keys, values


Members



    {% for key, user in users %}

                                                                                                                         {% endfor %}


Members

    {% for key, user in users %}
                                                                                       {% endfor %}





if tag
Needless to say, just look at the example {% if users %}

    {% for user in users %}
                                                                                                                                                          {% endfor %}

{% endif %}

{% if kenny.sick %}

Kenny is sick.

{% elseif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
{% if users %}


                {% for user in users %}
                                                           
  • {{ user.username|e }}

  •            {% endfor %}

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


{% macro input(name, value, type, size) %} 
     
{% endmacro %} 
{% macro input(name, value, type, size) %}
   
{% endmacro %}
macro与函数的不同之处在于:

1、参数的默认值是通过macro块内部的 default过滤器来定义的。

2、参数总是可选的。

另外,就跟php函数一样,macro内部是无法使用外部的变量的。但你可以传递一个特殊变量_context作为参数来获取整个内容。

macro可以被定义在任何的模板内,但在你使用之前需要使用 imported


{% import "forms.html" as forms %} 
{% import "forms.html" as forms %}然后就可以这样使用了


{{ forms.input('username') }}

 

{{ forms.input('password', null, 'password') }}

 

{{ forms.input('username') }}


{{ forms.input('password', null, 'password') }}

如果你要在定义macro的模板里使用,就不需要imported 可以使用特殊变量_self


{{ _self.input('username') }}

 

{{ _self.input('username') }}


如果你要定义一个macro里 包含另一个macro,并且两个macro在同一个文件里,可以使用特殊变量_self


{% macro input(name, value, type, size) %} 
   
{% endmacro %} 
 
{% macro wrapped_input(name, value, type, size) %} 
   

 
        {{ _self.input(name, value, type, size) }} 
   
 
{% endmacro %} 
{% macro input(name, value, type, size) %}
 
{% endmacro %}

{% macro wrapped_input(name, value, type, size) %}
   


        {{ _self.input(name, value, type, size) }}
   

{% endmacro %}
如果两个macro在不同的文件里,你需要使用import


{# forms.html #} 
 
{% macro input(name, value, type, size) %} 
   
{% endmacro %} 
 
{# shortcuts.html #} 
 
{% macro wrapped_input(name, value, type, size) %} 
    {% import "forms.html" as forms %} 
   

 
        {{ forms.input(name, value, type, size) }} 
   
 
{% endmacro %} 
{# forms.html #}

{% macro input(name, value, type, size) %}
 
{% endmacro %}

{# shortcuts.html #}

{% macro wrapped_input(name, value, type, size) %}
    {% import "forms.html" as forms %}
   


        {{ forms.input(name, value, type, size) }}
   

{% 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 %}
SOME TEXT
{% 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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template