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' %}
{% set foo, bar = 'foo', 'bar' %}
其中 'foo'~'bar' 这个我没怎么看明白,测试了一下,可能是字符串连接的。
set还有一种用法,就是把 块内的内容赋值给变量
{% set foo %}
extends标签
这个标签用来表示本模板继承自另外一个模板。和php一样,twig不支持多重继承,所以你只能有一个extends标签,而且要在模板的最上方。
我们先来定义一个“基模板” base.html 他就像一个骨架一个。
A sub-template might look like this
{% 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.
If the child template does not define block footer, the parent template will use the default value instead.
Note: The name of the block tag cannot be repeated. If you want the same block to be printed multiple times. You can use the block function
Parent block
Maybe you will need the content of the parent block. You can use the parent function, which is useful when you want to add content to a block instead of overwriting it.
{% block sidebar %}
Name endblock
The template engine allows you to name the closing tag, which improves readability a lot. But personally I don’t think it’s useful.
{% block sidebar %}
{% block inner_sidebar %}
...
{% endblock inner_sidebar %}
{% endblock sidebar %}
{% block sidebar %}
{% block inner_sidebar %}
...
{% endblock inner_sidebar %}
{% endblock sidebar %}
Allows you to nest generated blocks to form more complex blocks
{% for item in seq %}
Abbreviation block
The following two writing methods are equivalent
{% block title %}
{{ page_title|title }}
{% endblock %}
{% block title page_title|title %}
{% block title %}
{{ page_title|title }}
{% endblock %}
Dynamic inheritance
You can use a variable to inherit from different templates.
{% extends some_var %}
{% extends some_var %}
This is also OK if the variable is a twig template object.
$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));
Updated in version 1.2 You can pass an array and twig will select the first existing template to inherit.
{% extends ['layout.html', 'base_layout.html'] %}
{% extends ['layout.html', 'base_layout.html'] %}
Conditional inheritance
This is very simple, just check it out yourself,
{% extends standalone ? "minimum.html" : "base.html" %}
{% extends standalone ? "minimum.html" : "base.html" %}
block tag
See extends tag
include tag
Loads a template and returns rendered content. The loaded template can use the variables of the current template {% include 'header.html' %}
Body
{% include 'footer.html' %}
{% include 'header.html' %}
Body
{% include 'footer.html' %}
You can add variables to templates
{# 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 %}
You can also use the only keyword to prevent the loaded template from using the variables of the current template. You can only use the variables in the include {# 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 %}
The loaded template name can also be a twig expression
{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}
{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}
You can also use twig template object
$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));
Newly added in version 1.2, you can add the ignore missing keyword to the template so that no error will occur when the template does not exist.
{% 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 %} New content added in version 1.2, you can pass an array to include, and it will automatically load the first existing template {% include ['page_detailed.html', 'page .html'] %}
{% include ['page_detailed.html', 'page.html'] %}
import tag
Twig allows you to put some commonly used code into macros, which are imported by different templates.
There are two ways to import templates. You can import the entire template into a variable, or only import the few macros you need
Suppose we have an assistant module to help us render the form (forms.html)
{% macro input(name, value, type, size) %}
{% endmacro %}
{% macro textarea(name, value, rows) %}
{% macro textarea(name, value, rows) %}
{% import 'forms.html' as forms %}
{{ forms.textarea('comment') }}
{{ forms.textarea('comment') }}
{% from 'forms.html' import input as input_field, textarea %}
{{ textarea('comment') }}
{{ textarea('comment') }}
If it is a macro defined in the current template, there is no need to import it, just use the special variable _self
{# index.html template #}
{% macro textarea(name, value, rows) %}
{% macro textarea(name, value, rows) %}
{{ _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的专栏