Zend Framework 的页面布局模块——Zend_Layout——既可以跟 MVC 一起使用,也可以单独使用。本文只讨论与 MVC 一起使用的情况。 1. 布局脚本 在 application/views 下创建一个layouts 的文件夹。主布局脚本 layout.phtml 代码如下: doctype('XHTML1_STRICT') ?>
headTitle() ?> $this->headLink()->appendStylesheet("/styles/main.css"); // add more links ... ?> headLink() ?>
partial('leftcolumn.phtml') ?>
|
layout()->content ?>
|
除了layout.phtml 之外,还需要编写 header.phtml,leftcolumn.phtml,footer.phtml,以及 main.css 等文件。 Zend Framework 的文档中用一个视图表示了页面布局的应用。 2. 设置页面布局 在 MVC 下设置页面布局非常简单,编辑 html/index.php,加入下面两行代码: /** Setuplayout*/ require_once 'Zend/Layout.php'; Zend_Layout::startMvc($rootPath . '/application/views/layouts'); 注意:在启动页面布局后,要调整已有的各个页面,把不需要的 html 元素,如 等去掉。另外,可以通过 $this->headTitle() 来设置页面的题头。 改变页面的布局也很简单,只需在控制器中用下面的代码即可: $this->_helper->layout->setLayout('new_layout'); 如果一个控制器所有动作都使用同一个页面布局,可以通过控制器的初始化函数来设置: public function init() { parent::init(); $this->_helper->layout->setLayout('new_layout');
|