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'] %}
参考官方文档
相关文章推荐
- Outlook中撤回已发送的邮件
- React 入门实例教程
- 如何批量添加和删除SMTP地址
- Qt学习之路(56): 二进制文件读写
- UVA 10420 - List of Conquests
- Exchange2013 Get-MessageTrackingLog Powershell使用
- SCCM2012软件分发
- HTML网页开发学习笔记(燕十八)-37 整齐的表格 作业
- 微软私有云SCCM2012 R2培训
- CentOS Linux中,使用sendEmail发送163邮箱的邮件
- 腾云驾雾了解Windows10和微软私有云
- SPF记录详解
- RAID技术介绍和总结
- poj 几道简单的搜索题 (一)
- 在liunx中构建DNS主从服务器的配置文档
- 2016.7.12 夏令营 Day5
- linux配置Play Framework
- 注解/Annotation
- 微软公有云Azure对Microsoft软件兼容性支持
- UVA 10474 - Where is the Marble?