当前位置: 首页 > 图文教程 > 网络编程 > PHP > Zend Framework 入门——页面布局

PHP
使用.htaccess来防盗链
mysql 时间转换函数的用法
PHP中addcslashes函数与stripcslashes的用法
dedecms5.1升级sp1出现IfTagNull()错误解决方法记录
实现dedecms图集单击图片翻页的功能
动态网页制作PHP常用的正则表达式
PHP读取RSS feed源代码(带注释,可读取多个源)
PHP与正则表达系列之一: PHP中的正则表达式
给wordpress 增加干扰码防采集
使用PHP脚本修改Linux或Unix系统口令
PHP技巧:php过滤危险html代码
php配置文件php.ini的中文注释版
成就PHP高手的五个必由之路
PHP比ASP优秀的七个理由
如何正确理解PHP的错误信息
学习使用PHP数组
PHP中创建并处理图象
RPM安装PHP后应如何配置 Apache
PHP中的Java扩展
PHP4.04 新增加了专用的字符函数 Ctype

PHP 中的 Zend Framework 入门——页面布局


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 136 ::
收藏到网摘: n/a

Zend Framework 的页面布局模块——Zend_Layout——既可以跟 MVC 一起使用,也可以单独使用。本文只讨论与 MVC 一起使用的情况。

1. 布局脚本

在 application/views 下创建一个 layouts 的文件夹。主布局脚本 layout.phtml 代码如下:

<?php echo $this->doctype('XHTML1_STRICT') ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php echo $this->headTitle() ?>
<?php
$this->headLink()->appendStylesheet("/styles/main.css");
// add more links ...
?>
<?php echo $this->headLink() ?>
</head>
<body>
<div id="header">
<?php echo $this->partial('header.phtml') ?>
</div>
<table>
<tr>
<td valign=top>
<div id="leftcolumn">
<?php echo $this->partial('leftcolumn.phtml') ?>
</div>
</td>
<td valign=top>
<div id="content">
<?php echo $this->layout()->content ?>
</div>
</td>
</tr>
</table>
<div id="footer">
<?php echo $this->partial('footer.phtml') ?>
</div>
</body>
</html>

除了 layout.phtml 之外,还需要编写 header.phtml,leftcolumn.phtml,footer.phtml,以及 main.css 等文件。

Zend Framework 的文档中用一个视图表示了页面布局的应用。

 

2. 设置页面布局

在 MVC 下设置页面布局非常简单,编辑 html/index.php,加入下面两行代码:

/** Setup layout */
require_once 'Zend/Layout.php';
Zend_Layout::startMvc($rootPath . '/application/views/layouts');

注意:在启动页面布局后,要调整已有的各个页面,把不需要的 html 元素,如<header> <title> <body> 等去掉。另外,可以通过 $this->headTitle() 来设置页面的题头。

改变页面的布局也很简单,只需在控制器中用下面的代码即可:

$this->_helper->layout->setLayout('new_layout');

如果一个控制器所有动作都使用同一个页面布局,可以通过控制器的初始化函数来设置:

public function init() {
parent::init();

$this->_helper->layout->setLayout('new_layout');
}

 

相关文章

Zend Framework 入门——快速上手

Zend Framework 入门——多国语言支持

Zend Framework 入门——错误处理

Zend Framework 入门——页面布局