Zend Framework 1.x中Zend_Layout
转载请注明地址: http://www.phpddt.com/mvc/zend_layout.html 尊重他人劳动成果就是尊重自己!
就本人来说,不怎么喜欢Zend Framework,赞Symphony。Zend Framework 2也出来一段时间了,有时间研究下。今天有人问了我,看了下Zend_Layout使用部分。一个基本的web页面,可能页面的头和尾或某些模块都是一样,可以把公共的部分做成模版。不仅可以提高开发效率,也为后期的维护带来方便。还可以轻松实现切换主题机制。
第一步:在application.ini中配置layout路径,[production]下加入:
resources.layout.layoutPath = APPLICATION_PATH "/views/scripts/layouts"
resources.layout.layout = "layout"
文件结构如下:

第二步:建立相关文件
layout.phtml文件:
1 <!--www.phpddt.com--> 2 <?php echo $this->render('header.phtml');?> 3 <!--来一个布局变量--> 4 <?php echo $this->layout()->nav ?> 5 <!--默认的,$content 变量被应用程序的视图脚本呈现内容填充。--> 6 <?php echo $this->layout()->content;?> 7 <?php echo $this->render('footer.phtml');
?>
header.phtml文件:
1 <html> 2 <head> 3 <title>这是www.phpddt.com</title> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 </head> 6 <body>
nav.phtml文件:
1 <div id="nav" style="margin:20px 0;background-color: red;"> 2 这是导航 3 </div>
footer.phtml文件:
1 <div id="footer" style="background-color: #EEEEEE; height: 30px;"> 2 这是footer 3 </div> 4 </body> 5 </html>
第三步:在动作控制器中:
1 <?php 2 //@blog<http://www.phpddt.com> 3 require_once APPLICATION_PATH.'/models/Article.php'; 4 require_once 'BaseController.php'; 5 require_once 'Zend/Layout.php'; 6 class IndexController extends BaseController 7 { 8 public function init() 9 { 10 parent::init(); 11 //set layout 12 $layout = new Zend_Layout(); 13 $layout->nav = $this->view->render('layouts/nav.phtml'); 14 $layout->setLayout('layout'); 15 } 16 //这里的内容到$this->layout()->content中 17 public function indexAction() 18 { 19 $a = new Article(); 20 $res = $a->fetchAll()->toArray(); 21 $this->view->res = $res; 22 $this->render('index'); 23 } 24 }
通过访问,你可以看到,zend已经为你加载了全部内容:


浙公网安备 33010602011771号