Zend Framework's page layout module - Zend_Layout - can be used together with MVC or alone. This article only discusses use with MVC.
1. The layout script
creates a layouts folder under application/views. The main layout script layout.phtml code is as follows:
<?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>
In addition to layout.phtml, you also need to write files such as header.phtml, leftcolumn.phtml, footer.phtml, and main.css.
The Zend Framework documentation uses a view to represent the application of page layout.
2. Set the page layout.
Setting the page layout under MVC is very simple. Edit html/index.php and add the following two lines of code:
/** Setuplayout*/
require_once 'Zend/Layout.php';
Zend_Layout::startMvc($rootPath . '/application/views/layouts');
Note: After starting the page layout, you need to adjust the existing pages and remove unnecessary html elements, such as <header> <title> <body >Wait to remove. In addition, you can set the title of the page through $this->headTitle().
Changing the layout of the page is also very simple, just use the following code in the controller:
$this->_helper->layout->setLayout('new_layout');
If all actions of a controller use the same page layout , which can be set through the controller's initialization function:
public function init() {
parent::init();
$this->_helper->layout->setLayout('new_layout');
}