
日期:2008-07-02 作者:喜騰小二 來源:PHPChina
Zend Framework 的页面佈局模組——Zend_Layout——既可以跟 MVC 一起使用,也可以單獨使用。本文隻討論與 MVC 一起使用的情況。
1. 佈局指令檔
在 application/views 下建立一個 layouts 的檔案夾。主佈局指令檔 layout.phtml 程式碼如下:
doctype('XHTML1_STRICT') ?>
partial('leftcolumn.phtml') ?> |
除了 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 元素,如
改變页面的佈局也很簡單,隻需在控制器中用下麵的程式碼即可:
$this->_helper->layout->setLayout('new_layout');
如果一個控制器所有動作都使用同一個页面佈局,可以透過控制器的初始化函式來設定:
public function init() {
parent::init();
$this->_helper->layout->setLayout('new_layout');
}