Learning Twig's tags (Chinese) Part 2_PHP Tutorial

WBOY
Release: 2016-07-13 17:48:03
Original
988 people have browsed it

set标签
主要是用来给变量赋值的。

 

 

{% set foo = 'foo' %} 
 
{% set foo = [1, 2] %} 
 
{% set foo = {'foo': 'bar'} %} 
 
{% set foo = 'foo' ~ 'bar' %} 
 
{% set foo, bar = 'foo', 'bar' %} 
{% set foo = 'foo' %}

{% set foo = [1, 2] %}

{% set foo = {'foo': 'bar'} %}

{% set foo = 'foo' ~ 'bar' %}

{% set foo, bar = 'foo', 'bar' %}
其中 'foo'~'bar' 这个我没怎么看明白,测试了一下,可能是字符串连接的。

set还有一种用法,就是把 块内的内容赋值给变量


{% set foo %} 
 

 
{% endset %} 
{% set foo %}
 
{% endset %}


extends标签
这个标签用来表示本模板继承自另外一个模板。和php一样,twig不支持多重继承,所以你只能有一个extends标签,而且要在模板的最上方。

我们先来定义一个“基模板” base.html 他就像一个骨架一个。


 
 
     
        {% block head %} 
             
            {% block title %}{% endblock %} - My Webpage 
        {% endblock %} 
     
     
       

{% block content %}{% endblock %}
 
         
     
 


   
        {% block head %}
           
            {% block title %}{% endblock %} - My Webpage
        {% endblock %}
   
   
       
{% block content %}{% endblock %}

       


The {% block %} tag defines 4 blocks (block head, block title, block content, block footer), which allows sub-templates to fill in content. The function of block is to tell the template engine that the content inside can be overwritten by sub-templates.

A sub-template might look like this


{% extends "base.html" %}

{% block title %}Index{% endblock %}
{% block head %}
{{ parent() }}

{% endblock %}
{% block content %}

Index



          Welcome on my awesome homepage.


{% endblock %}
{% extends "base.html" %}

{% block title %}Index{% endblock %}
{% block head %}
{{ parent() }}

{% endblock %}
{% block content %}

Index



          Welcome on my awesome homepage.


{% endblock %}
extends is very critical. It tells the template engine that this template inherits from another template (base.html). When the template engine parses this template, it will first load the parent template. The extends tag should be the first tag within the template.

If the child template does not define block footer, the parent template will use the default value instead.


Note: The name of the block tag cannot be repeated. If you want the same block to be printed multiple times. You can use the block function


{% block title %}{% endblock %}

{{ block('title') }}


{% block body %}{% endblock %}
{% block title %}{% endblock %}

{{ block('title') }}


{% block body %}{% endblock %}

Parent block
Maybe you will need the content of the parent block. You can use the parent function, which is useful when you want to add content to a block instead of overwriting it.


{% block sidebar %}

Table Of Contents


...
{{ parent() }}
{% endblock %}
{% block sidebar %}

Table Of Contents


...
{{ parent() }}
{% endblock %}


Name endblock
The template engine allows you to name the closing tag, which improves readability a lot. But personally I don’t think it’s useful.

{% block sidebar %}
{% block inner_sidebar %}
               ... {% endblock inner_sidebar %}
{% endblock sidebar %}
{% block sidebar %}
{% block inner_sidebar %}
...
{% endblock inner_sidebar %}
{% endblock sidebar %}

Nested block

Allows you to nest generated blocks to form more complex blocks

{% for item in seq %}

  • {% block loop_item %}{{ item }}{% endblock %}

  • {% endfor %}
    {% for item in seq %}
  • {% block loop_item %}{{ item }}{% endblock %}

  • {% endfor %}

    Abbreviation block
    The following two writing methods are equivalent

    {% block title %}
    {{ page_title|title }}
    {% endblock %}

    {% block title page_title|title %}
    {% block title %}
    {{ page_title|title }}
    {% endblock %}

    {% block title page_title|title %}

    Dynamic inheritance
    You can use a variable to inherit from different templates.

    {% extends some_var %}
    {% extends some_var %}
    This is also OK if the variable is a twig template object.


    $layout = $twig->loadTemplate('some_layout_template.twig');

    $twig->display('template.twig', array('layout' => $layout));
    $layout = $twig->loadTemplate('some_layout_template.twig');

    $twig->display('template.twig', array('layout' => $layout));
    Updated in version 1.2 You can pass an array and twig will select the first existing template to inherit.


    {% extends ['layout.html', 'base_layout.html'] %}
    {% extends ['layout.html', 'base_layout.html'] %}


    Conditional inheritance
    This is very simple, just check it out yourself,


    {% extends standalone ? "minimum.html" : "base.html" %}
    {% extends standalone ? "minimum.html" : "base.html" %}

    block tag
    See extends tag

    include tag
    Loads a template and returns rendered content. The loaded template can use the variables of the current template {% include 'header.html' %}
    Body
    {% include 'footer.html' %}
    {% include 'header.html' %}
    Body
    {% include 'footer.html' %}

    You can add variables to templates


    {# the foo template will have access to the variables from the current context and the foo one #}
    {% include 'foo' with {'foo': 'bar'} %}

    {% set vars = {'foo': 'bar'} %}
    {% include 'foo' with vars %}
    {# the foo template will have access to the variables from the current context and the foo one #}
    {% include 'foo' with {'foo': 'bar'} %}

    {% set vars = {'foo': 'bar'} %}
    {% include 'foo' with vars %}
    You can also use the only keyword to prevent the loaded template from using the variables of the current template. You can only use the variables in the include {# only the foo variable will be accessible #}
    {% include 'foo' with {'foo': 'bar'} only %}

    {# no variable will be accessible #}
    {% include 'foo' only %}
    {# only the foo variable will be accessible #}
    {% include 'foo' with {'foo': 'bar'} only %}

    {# no variable will be accessible #}
    {% include 'foo' only %}

    The loaded template name can also be a twig expression


    {% include some_var %}
    {% include ajax ? 'ajax.html' : 'not_ajax.html' %}
    {% include some_var %}
    {% include ajax ? 'ajax.html' : 'not_ajax.html' %}
    You can also use twig template object


    $template = $twig->loadTemplate('some_template.twig');

    $twig->loadTemplate('template.twig')->display(array('template' => $template));
    $template = $twig->loadTemplate('some_template.twig');

    $twig->loadTemplate('template.twig')->display(array('template' => $template));

    Newly added in version 1.2, you can add the ignore missing keyword to the template so that no error will occur when the template does not exist.

    {% include "sidebar.html" ignore missing %}
    {% include "sidebar.html" ignore missing with {'foo': 'bar} %}
    {% include "sidebar.html" ignore missing only %}
    {% include "sidebar.html" ignore missing %}
    {% include "sidebar.html" ignore missing with {'foo': 'bar} %}
    {% include "sidebar.html" ignore missing only %} New content added in version 1.2, you can pass an array to include, and it will automatically load the first existing template {% include ['page_detailed.html', 'page .html'] %}
    {% include ['page_detailed.html', 'page.html'] %}

    import tag


    Twig allows you to put some commonly used code into macros, which are imported by different templates.

    There are two ways to import templates. You can import the entire template into a variable, or only import the few macros you need

    Suppose we have an assistant module to help us render the form (forms.html)


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

    {% endmacro %}

    {% macro textarea(name, value, rows) %}
     
    {% endmacro %} 
     
    {% import _self as forms %} 
     

    {{ forms.textarea('comment') }}

     
    {# index.html template #}

    {% macro textarea(name, value, rows) %}
       
    {% endmacro %}

    {% import _self as forms %}

    {{ forms.textarea('comment') }}


    from标签
    参见 import标签

    摘自 jiaochangyun的专栏
     

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478460.htmlTechArticleset标签 主要是用来给变量赋值的。 {% set foo = foo %} {% set foo = [1, 2] %} {% set foo = {foo: bar} %} {% set foo = foo ~ bar %} {% set foo, bar = foo, bar %} {% set...
    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