您的位置:首页 > 其它

twig引用模板

2016-07-13 16:24 288 查看

controller内容:

public function index() {

return $this->render();
}


view\index.html内容:

引用三个页面

{% include 'head.html' %}
{% include 'content.html' %}
{% include 'footer.html' %}


head.html内容:

<!DOCTYPE html>
<html>
<head>
<title>This Title</title>
</head>


content.html内容:

<body>
<div id="content">This Content</div>
</body>


footer.html内容:

<script type="text/javascript">
alert('This Footer');
</script>
</html>


解析后的内容:

<html>
<head>
<title>This Title</title>
</head>

<body>
<div id="content">This Content</div>

<script type="text/javascript">
alert('This Footer');
</script>
</body>
</html>


变量使用:

controller内传递到模板中使用:

public function index() {

return $this->render(
array(
'time'=>date("Y-m-d H:i:s"),

)
);
}


<div id="time">当前时间是:{{time}}</div>


四个页面每个页面都可以使用。

引用赋值

可在footer.html中使用param_time

{% include 'footer.html' with {'param_time': time} %}


模板内set赋值参数:

{% set title = '这是标题'%}
<title>{{title}}</title>


禁用变量

{% set time_param = time %}
{% include 'head.html' only%}
{% include 'content.html' only %}
{% include 'footer.html' %}
<!--body中使用没有任何效果-->
<body>
<div id="content">This Content</div>
<div id="time">当前时间是:{{time_param}}</div>
<div id="time">当前时间是:{{time}}</div>
</body>


引用的模板不存在

下面的代码可以忽略报错

{% include 'sidebar.html' ignore missing %}


数组方式传入要引用的模板

检测模板是否存在,存在则引用,不存在则忽略。

{% include ['content.html', 'footer.html'] %}


参考官方文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: