Zend Framework 1.x中Zend_Layout使用教程(实现视图布局)

来源:互联网 发布:淘宝卖手机3c证书编号 编辑:程序博客网 时间:2024/06/09 13:52

就本人来说,不怎么喜欢Zend Framework,赞Symphony。Zend Framework2也出来一段时间了,有时间研究下。今天有人问了我,看了下Zend_Layout使用部分。一个基本的web页面,可能页面的头和尾或某些模块都是一样,可以把公共的部分做成模版。不仅可以提高开发效率,也为后期的维护带来方便。还可以轻松实现切换主题机制。


第一步:在application.ini中配置layout路径,[production]下加入:
resources.layout.layoutPath = APPLICATION_PATH "/views/scripts/layouts"
resources.layout.layout = "layout"
文件结构如下:


第二步:建立相关文件


layout.phtml文件:

<!--www.phpddt.com--><?php echo $this->render('header.phtml');?><!--来一个布局变量--><?php echo $this->layout()->nav ?><!--默认的,$content 变量被应用程序的视图脚本呈现内容填充。--><?php echo $this->layout()->content;?><?php echo $this->render('footer.phtml');?>


header.phtml文件:

<html>    <head>        <title>这是www.phpddt.com</title>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    </head>    <body>

nav.phtml文件:

<div id="nav" style="margin:20px 0;background-color: red;">    这是导航</div>

footer.phtml文件:

<div id="footer" style="background-color: #EEEEEE; height: 30px;">这是footer</div></body></html>

第三步:在动作控制器中:

<?php//@blog<http://www.phpddt.com>require_once APPLICATION_PATH.'/models/Article.php';require_once 'BaseController.php';require_once 'Zend/Layout.php';class IndexController extends BaseController{    public function init()    {        parent::init();        //set layout        $layout = new Zend_Layout();        $layout->nav = $this->view->render('layouts/nav.phtml');        $layout->setLayout('layout');    }    //这里的内容到$this->layout()->content中    public function indexAction()    {        $a = new Article();        $res = $a->fetchAll()->toArray();        $this->view->res = $res;        $this->render('index');    }}

通过访问,你可以看到,zend已经为你加载了全部内容:


转载来源:http://www.phpddt.com/mvc/zend_layout.html

0 0
原创粉丝点击