Home Backend Development PHP Tutorial Twig's tags learning (Chinese) Part 3 Completed_PHP Tutorial

Twig's tags learning (Chinese) Part 3 Completed_PHP Tutorial

Jul 13, 2016 pm 05:48 PM
twig use Chinese host content study yes Label Add to Version of want this

use标签
use标签是1.1版本新添加内容。
这个use标签主要是来解决模板只能从一个父模板继承,而你又想重用其他模板的问题。但是use标签只会导入block区块,
(注意import只会导入宏macros,include会导入一切。这三个标签要区分清楚)
比如 {% extends "base.html" %} 
 
{% use "blocks.html" %} 
 
{% block title %}{% endblock %} 
{% block content %}{% endblock %} 
{% extends "base.html" %}

{% use "blocks.html" %}

{% block title %}{% endblock %}
{% block content %}{% endblock %}而blocks.html的内容是
# blocks.html 
{% block sidebar %}{% endblock %} 
# blocks.html
{% block sidebar %}{% endblock %}我们从blocks..html导入了 block sidebar
运行的结果几乎等于
{% extends "base.html" %} 
 
{% block sidebar %}{% endblock %} 
{% block title %}{% endblock %} 
{% block content %}{% endblock %} 
{% extends "base.html" %}

{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
要注意,被use标签导入的模板(上例中的block.html),不能再继承别的模板,不能定义宏macros。但它可以再use其他模板。
另外use标签后面的文件名,不能是一个表达式。


当被导入了的block和主模板的block重名了,模板引擎会自动忽略被use标签导入block。
为了避免这种情况。你可以在使用use标签的时候,给block重命名
{% extends "base.html" %} 
 
{% use "blocks.html" with sidebar as base_sidebar %} 
 
{% block sidebar %}{% endblock %} 
{% block title %}{% endblock %} 
{% block content %}{% endblock %} 
{% extends "base.html" %}

{% use "blocks.html" with sidebar as base_sidebar %}

{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
1.3版本新支持了 parent()函数,(这个特别重要)
parent()函数,会自动的搞定block的继承树,如果你在主模板里覆盖了use标签引入进来的block块,而用parent()函数则可以调用被覆盖的那个block内容
{% extends "base.html" %} 
 
{% use "blocks.html" %} 
 
{% block sidebar %} 
    {{ parent() }} 
{% endblock %} 
 
{% block title %}{% endblock %} 
{% block content %}{% endblock %} 
{% extends "base.html" %}

{% use "blocks.html" %}

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

{% block title %}{% endblock %}
{% block content %}{% endblock %}
注意,parent()的内容 其实是blocks.html里的block sidebar的内容。因为继承树是  base.html->blocks.html->本模板


如果你在use标签里给导入的block重命名了,那就可以使用block函数,来代替上面代码中的parent函数所达到的效果
{% extends "base.html" %} 
 
{% use "blocks.html" with sidebar as parent_sidebar %} 
 
{% block sidebar %} 
    {{ block('parent_sidebar') }} 
{% endblock %} 
{% extends "base.html" %}

{% use "blocks.html" with sidebar as parent_sidebar %}

{% block sidebar %}
    {{ block('parent_sidebar') }}
{% endblock %}
你可以使用任意数量的use标签,如果多个use标签里的block名字存在重复,那么最后use的那个有效。
spacelsee标签
会删除html标签之间的空白
{% spaceless %} 
    <div> 
        <strong>foo</strong> 
    </div> 
{% endspaceless %} 
 
{# output will be <div><strong>foo</strong></div> #} 
{% spaceless %}
    <div>
        <strong>foo</strong>
    </div>
{% endspaceless %}

{# output will be <div><strong>foo</strong></div> #}
autoescape tag
This is so embarrassing that I didn’t understand it. I just know it literally means auto-escaping. . but. . When I was doing the experiment, I still didn’t know how to use it
The official example he gave is
{% autoescape true %}
Everything will be automatically escaped in this block
{% endautoescape %}

{% autoescape false %}
Everything will be outputed as is in this block
{% endautoescape %}

{% autoescape true js %}
Everything will be automatically escaped in this block
Using the js escaping strategy
{% endautoescape %}
{% autoescape true %}
Everything will be automatically escaped in this block
{% endautoescape %}

{% autoescape false %}
Everything will be outputed as is in this block
{% endautoescape %}

{% autoescape true js %}
Everything will be automatically escaped in this block
Using the js escaping strategy
{% endautoescape %}
And I tested it this way. The output is still the original content.
{% autoescape true %}
<body><b>aaaa</b></body>
{% endautoescape %}

{% autoescape false %}
<b>aaaa</b>
{% endautoescape %}

{% autoescape true js %}
<script>
Function aaa(){alert('x');}
</script>
{% endautoescape %}
{% autoescape true %}
<body><b>aaaa</b></body>
{% endautoescape %}

{% autoescape false %}
<b>aaaa</b>
{% endautoescape %}

{% autoescape true js %}
<script>
Function aaa(){alert('x');}
</script>
{% endautoescape %}
I’d like to ask all the senior brothers who are passing by to ask this question. . .
His official documentation also says that if {% autoescape true %} is used, the content inside will be escaped into safe content unless you use the raw filter.
{% autoescape true %}
{{ safe_value|raw }}
{% endautoescape %}
{% autoescape true %}
{{ safe_value|raw }}
{% endautoescape %}
In addition, the return values ​​​​of functions in twig are safe, such as macros parent


raw tag
The raw tag ensures that the data in the block will not be parsed by the template engine. {% raw %}
<ul>
{% for item in seq %}
                                                                                                        {% endfor %}
</ul>
{% endraw %}
{% raw %}
<ul>
{% for item in seq %}
                                                                                                  {% endfor %}
</ul>
{% endraw %}
flush tag
New content in version 1.5
Tell the template to refresh the output cache. Internally, it actually calls PHP's flush function
{% flush %}
{% flush %}

do tag

New content in version 1.5

The do tag functions like an output tag {{ }}. It can calculate some expressions, but the difference is that it does not print anything
{% do 1 + 2 %}
{% do 1 + 2 %}

This concludes the study of labels. Encouraged by applause. . . . . Next enter the learning of filters. . . . . . Chirp Chirp Chirp


Excerpted from jiaochangyun’s column

http://www.bkjia.com/PHPjc/478459.html

truehttp: //www.bkjia.com/PHPjc/478459.htmlTechArticleuse tag The use tag is a new addition in version 1.1. This use tag is mainly used to solve the problem that templates can only inherit from one parent template, and you want to reuse other templates. But the use tag only...
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

How to add a TV to Mijia How to add a TV to Mijia Mar 25, 2024 pm 05:00 PM

How to add a TV to Mijia

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

Interpret the meaning and difference of PHP version NTS Interpret the meaning and difference of PHP version NTS Mar 27, 2024 am 11:48 AM

Interpret the meaning and difference of PHP version NTS

750,000 rounds of one-on-one battle between large models, GPT-4 won the championship, and Llama 3 ranked fifth 750,000 rounds of one-on-one battle between large models, GPT-4 won the championship, and Llama 3 ranked fifth Apr 23, 2024 pm 03:28 PM

750,000 rounds of one-on-one battle between large models, GPT-4 won the championship, and Llama 3 ranked fifth

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

Outlook stuck on adding account [Fixed] Outlook stuck on adding account [Fixed] Mar 23, 2024 pm 12:21 PM

Outlook stuck on adding account [Fixed]

Which version of Douyin matrix management system is recommended? How to do matrix marketing? Which version of Douyin matrix management system is recommended? How to do matrix marketing? Mar 21, 2024 pm 03:50 PM

Which version of Douyin matrix management system is recommended? How to do matrix marketing?

See all articles