Twig 的 tags学习(中文) 之二(4)
{% 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)
相关新闻>>
- 发表评论
-
- 最新评论 更多>>