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' %}
立即学习“PHP免费学习笔记(深入)”;
{% set foo, bar = 'foo', 'bar' %}
其中 'foo'~'bar' 这个我没怎么看明白,测试了一下,可能是字符串连接的。
set还有一种用法,就是把 块内的内容赋值给变量
{% set foo %} 
  
extends标签
这个标签用来表示本模板继承自另外一个模板。和php一样,twig不支持多重继承,所以你只能有一个extends标签,而且要在模板的最上方。
我们先来定义一个“基模板” base.html 他就像一个骨架一个。
 
 
    
一个子模板大概类似于这样的
{% extends "base.html" %} 
 
{% block title %}Index{% endblock %} 
{% block head %} 
    {{ parent() }} 
     
{% endblock %} 
{% block content %} 
    
 
        Welcome on my awesome homepage. 
    
{% block title %}Index{% endblock %}
{% block head %}
    {{ parent() }}
    
{% endblock %}
{% block content %}
    
        Welcome on my awesome homepage.
    
如果子模板没有定义block footer ,那么父模板会用默认值代替。
注意:block标签的名字是不能重复的。如果你想让同一个block多次打印。可以使用block函数
父block
也许你会需要 父block的内容。可以使用parent函数,这很有用比如你想往一个block里添加内容而不是覆盖时。
{% block sidebar %} 
    
命名endblock
模板引擎 允许你命名结束标记,这样可读性会提高很多。但个人觉得没啥用处。
{% block sidebar %} 
    {% block inner_sidebar %} 
        ... 
    {% endblock inner_sidebar %} 
{% endblock sidebar %} 
{% block sidebar %}
    {% block inner_sidebar %}
        ...
    {% endblock inner_sidebar %}
{% endblock sidebar %}
嵌套block
允许你嵌套生成block ,来形成更复杂的block
{% for item in seq %} 
    
简写block
以下这两种写法是等效的
{% block title %} 
    {{ page_title|title }} 
{% endblock %} 
 
{% block title page_title|title %} 
{% block title %}
    {{ page_title|title }}
{% endblock %}
{% block title page_title|title %}
动态继承
你可以用一个变量来继承不同的模板。
{% extends some_var %} 
{% extends some_var %}
如果变量是一个twig模板对象,也可以。
$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));
1.2版本更新 你可以传递一个数组,twig会选择第一个存在的模板,来继承。
{% extends ['layout.html', 'base_layout.html'] %} 
{% extends ['layout.html', 'base_layout.html'] %}
条件继承
这个很简单自己看吧,
{% extends standalone ? "minimum.html" : "base.html" %} 
{% extends standalone ? "minimum.html" : "base.html" %}
block标签
参见 extends标签
include标签
载入一个模板,返回渲染的内容。载入的模板可以使用当前模板的变量{% include 'header.html' %} 
    Body 
{% include 'footer.html' %} 
{% include 'header.html' %}
    Body
{% include 'footer.html' %}
你可以给模板添加变量
{# 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 %}
你也可以使用 only 关键字 来禁止载入的模板使用当前模板的变量,只能使用include 时with的变量{# 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 %}
载入的模板名也可以是一个twig表达式
{% include some_var %} 
{% include ajax ? 'ajax.html' : 'not_ajax.html' %} 
{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}
也可以用twig模板对象
$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));
1.2版本新加内容,可以在模板加上 ignore missing 关键字,这样当模板不存在的时候就不会引发错误。
{% 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 %}1.2版本新加内容,你可以给include传递一个数组,他会自动载入第一个存在的模板{% include ['page_detailed.html', 'page.html'] %} 
{% include ['page_detailed.html', 'page.html'] %}
import 标签
twig允许把一些常用的代码放入到macros(宏)里,这些macros被不同的模板导入。
有两种方法导入模板,你可以导入整个模板到一个变量里,或者只导入需要的几个macros
假如我们有个助手模块,来帮助我们渲染表单(forms.html)
{% macro input(name, value, type, size) %} 
     
{% endmacro %} 
 
{% macro textarea(name, value, rows) %} 
     
{% endmacro %} 
{% macro input(name, value, type, size) %}
    
{% endmacro %}
{% macro textarea(name, value, rows) %}
    
{% endmacro %}
最简单,最灵活的办法就是导入整个模板。(把模板导入到 forms变量里)
{% import 'forms.html' as forms %} 
 
{{ forms.textarea('comment') }}
{{ forms.textarea('comment') }}
{% from 'forms.html' import input as input_field, textarea %} 
 
{{ textarea('comment') }}
{{ textarea('comment') }}
如果是当前模板内定义的macros,那就不必导入了,直接使用特殊变量_self
{# index.html template #} 
 
{% macro textarea(name, value, rows) %} 
     
{% endmacro %} 
 
{{ _self.textarea('comment') }}
{% macro textarea(name, value, rows) %}
    
{% endmacro %}
{{ _self.textarea('comment') }}
{# index.html template #} 
 
{% macro textarea(name, value, rows) %} 
     
{% endmacro %} 
 
{% import _self as forms %} 
 
{{ forms.textarea('comment') }}
{% macro textarea(name, value, rows) %}
    
{% endmacro %}
{% import _self as forms %}
{{ forms.textarea('comment') }}
from标签
参见 import标签
摘自 jiaochangyun的专栏
 
                        
                        PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号