Twig 的 tags学习(中文) 之三 完结_PHP教程
Jul 13, 2016 pm 05:48 PM
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的专栏

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Comment définir le chinois dans le jeu mobile Call of Duty: Warzone

Comment ajouter un téléviseur à Mijia

Configurer le chinois avec VSCode : le guide complet

Interpréter le sens et la différence de la version PHP NTS

750 000 rounds de bataille en tête-à-tête entre grands modèles, GPT-4 a remporté le championnat et Llama 3 s'est classé cinquième

Conseils pour résoudre les caractères chinois tronqués lors de l'écriture de fichiers txt avec PHP
![Outlook bloqué lors de l'ajout d'un compte [Corrigé]](https://img.php.cn/upload/article/000/887/227/171116770937641.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Outlook bloqué lors de l'ajout d'un compte [Corrigé]

Quelle version du système de gestion matricielle Douyin est recommandée ? Comment faire du marketing matriciel ?
