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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software