Zend Framework 1.x中Zend_Layout使用教程(实现视图布局)
2014-07-24 11:24
781 查看
就本人来说,不怎么喜欢Zend Framework,赞Symphony。Zend Framework2也出来一段时间了,有时间研究下。今天有人问了我,看了下Zend_Layout使用部分。一个基本的web页面,可能页面的头和尾或某些模块都是一样,可以把公共的部分做成模版。不仅可以提高开发效率,也为后期的维护带来方便。还可以轻松实现切换主题机制。
![](https://img-blog.csdn.net/20140724111720296?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb19iYWk2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
第一步:在application.ini中配置layout路径,[production]下加入:
resources.layout.layoutPath = APPLICATION_PATH "/views/scripts/layouts"
resources.layout.layout = "layout"
文件结构如下:
![](https://img-blog.csdn.net/20140724112111132?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb19iYWk2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
第二步:建立相关文件
layout.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已经为你加载了全部内容:
![](https://img-blog.csdn.net/20140724112325078?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb19iYWk2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
转载来源:http://www.phpddt.com/mvc/zend_layout.html
第一步:在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
相关文章推荐
- Zend Framework 怎样使用Zend_View与Zend_Layout玩布局?
- Zend Framework教程之Zend_Layout布局助手详解
- Zend Framework教程之Zend_Layout布局助手详解
- Zend Framework教程-Zend_Helpers-视图助手-视图助手的实现原理
- Zend Framework教程-Zend_Layout-布局\助手Layout
- Android Layout中使用include标签实现UI布局重用
- Zend Framework教程-Zend_Helpers-视图助手(View_Helper)概述
- 无废话MVC入门教程四[视图中的Layout使用]
- Zend Framework教程-Zend的Model-基本规则和使用
- 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局
- XML使用include实现布局(layout)复用
- 无废话MVC入门教程四[视图中的Layout使用]
- android 使用代码实现 RelativeLayout布局
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 无废话MVC入门教程四[视图中的Layout使用]
- Zend Framework教程-Zend_Helpers-视图助手-创建自己的视图助手-demos2
- Zend Framework教程-Zend_Helpers-视图助手-常见的视图助手-其它
- Zend Framework教程-Zend_Helpers-视图助手-常见的视图助手-其它
- 使用include实现布局(layout)复用