Twig 的 tags学习(中文) 之三 完结
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 %}
foo
{% endspaceless %}
{# output will be
{% spaceless %}
foo
{% endspaceless %}
{# output will be
autoescape标签
这个十分汗颜,我居然没看懂。我只知道字面上的意思是自动转义。。但是。。我做实验的时候 还是不知道应怎么使用
他官方给的例子是
{% 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 %}
而我这么测试。输出的还是原本的内容。
{% autoescape true %}
{% endautoescape %}
{% autoescape false %}
aaaa
{% endautoescape %}
{% autoescape true js %}
<script> <br /> function aaa(){alert('x');} <br /> </script>
{% endautoescape %}
{% autoescape true %}
aaaa
{% endautoescape %}
{% autoescape false %}
aaaa
{% endautoescape %}
{% autoescape true js %}
<script><br />
function aaa(){alert('x');}<br />
</script>
{% endautoescape %}
这个请教各位路过的师兄了。。。
他官方文档还说,如果使用了 {% autoescape true %} 那么里面的内容都会被转义成安全的内容,除非你使用raw过滤器。
{% autoescape true %}
{{ safe_value|raw }}
{% endautoescape %}
{% autoescape true %}
{{ safe_value|raw }}
{% endautoescape %}
另外,twig里函数的返回值都是安全的比如 macros parent
raw标签
raw标签,保证区块内的数据不被模板引擎解析。{% raw %}
- {{ item }}
{% for item in seq %}
{% endfor %}
{% endraw %}
{% raw %}
- {{ item }}
{% for item in seq %}
{% endfor %}
{% endraw %}
flush标签
1.5版本新增内容
告诉模板,刷新输出缓存,在内部其实是调用了php的flush函数
{% flush %}
{% flush %}
do 标签
1.5版本新增内容
do 标签的作用就像是输出标签一样{{ }},他可以计算一些表达式,区别是不打印出任何东西
{% do 1 + 2 %}
{% do 1 + 2 %}
标签的学习到此结束了。掌声鼓励下。。。。。下面进入过滤器的学习。。。。。。呱唧呱唧
摘自 jiaochangyun的专栏

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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



Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Many users are increasingly favoring the electronic ecosystem of Xiaomi smart home interconnection in modern life. After connecting to the Mijia APP, you can easily control the connected devices with your mobile phone. However, many users still don’t know how to add Mijia to their homes. app, then this tutorial guide will bring you the specific connection methods and steps, hoping to help everyone in need. 1. After downloading Mijia APP, create or log in to Xiaomi account. 2. Adding method: After the new device is powered on, bring the phone close to the device and turn on the Xiaomi TV. Under normal circumstances, a connection prompt will pop up. Select "OK" to enter the device connection process. If no prompt pops up, you can also add the device manually. The method is: after entering the smart home APP, click the 1st button on the lower left

Regarding Llama3, new test results have been released - the large model evaluation community LMSYS released a large model ranking list. Llama3 ranked fifth, and tied for first place with GPT-4 in the English category. The picture is different from other benchmarks. This list is based on one-on-one battles between models, and the evaluators from all over the network make their own propositions and scores. In the end, Llama3 ranked fifth on the list, followed by three different versions of GPT-4 and Claude3 Super Cup Opus. In the English single list, Llama3 overtook Claude and tied with GPT-4. Regarding this result, Meta’s chief scientist LeCun was very happy and forwarded the tweet and

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

The meaning and difference of PHP version NTS PHP is a popular server-side scripting language that is widely used in the field of web development. There are two main versions of PHP: ThreadSafe(TS) and Non-ThreadSafe(NTS). On the official website of PHP, we can see two different PHP download versions, namely PHPNTS and PHPTS. So, what does PHP version NTS mean? What is the difference between it and the TS version? Next,

With the popularity of Douyin, more and more companies and individuals have begun to use Douyin for marketing. As an important tool for Douyin marketing, the Douyin matrix management system has also received widespread attention. So, which version of Douyin matrix management system is recommended? This article will answer this question for you and introduce how to play Douyin matrix marketing. 1. Which version of Douyin matrix management system is recommended? There are many versions of the Douyin matrix management system. The following versions are currently popular on the market: Douyin’s official version of the matrix management system is specially designed for companies with certain operating experience and budget, and can realize multi-account management, data Analysis and material library functions. This system provides an efficient way to manage Douyin accounts and helps companies better understand their operational data to better

1. Open the Douyin app and click [Me] in the lower right corner to enter the personal page. 2. Click the [Three Stripes] icon in the upper right corner and select the [Settings] option in the pop-up menu bar. 3. In the settings page, scroll to the bottom to view the current version number information of Douyin.
