While waiting for their first release of zend_layout, I'm currently using some workaround. What I want for my layout is to be able to shift preferred layout per Module, Controller, or Action.
First I have created class Xin_Layout
PHP Code:
<?php
class Xin_Layout {
const DEFAULT_PATH = 'layouts';
const DEFAULT_NAME = 'default';
protected $_hasLayout;
protected $_name;
protected $_path;
public function __construct() {
$this->_path = self::DEFAULT_PATH;
$this->_name = self::DEFAULT_NAME;
$this->_hasLayout = true;
}
public function disableLayout() {
$this->_hasLayout = false;
}
public function enableLayout() {
$this->_hasLayout = true;
}
public function hasLayout() {
return $this->_hasLayout;
}
public function setName($name) {
$this->_name = $name;
}
public function setPath($path) {
$this->_path = $path;
}
public function getName() {
return $this->_name;
}
public function getPath() {
return $this->_path;
}
}
?>
Then extend Zend_Controller_Action to use Xin_Layout
PHP Code:
<?php
require_once('Zend/Controller/Action.php');
require_once('Xin/Layout.php');
class Xin_Controller_Action extends Zend_Controller_Action {
protected $_layout;
public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) {
$this->_layout = new Xin_Layout();
parent::__construct($request, $response, $invokeArgs);
}
public function postDispatch() {
if ( $this->_layout->hasLayout() ) {
$file = $this->_request->getParam('controller') .'/'.
$this->_request->getParam('action').'.'.$this->viewSuffix;
$this->view->content_for_layout = $this->view->render($file);
$layout = $this->_layout->getName();
$path = $this->_layout->getPath();
$this->renderScript($path.'/'.$layout.'.'.$this->viewSuffix);
}
}
}
?>
As you can see, layout path is based under controller base path.
Note that rendering was done under postDistpatch().
Sample Usage:
Directories:
File(s)
application-->controllers:
IndexController.php
application-->views-->scripts-->index:
index.pthml
application-->views-->scripts-->layouts:
default.phtml
customize.phtml
html:
index.php
application/controllers/IndexController.php file
PHP Code:
<?php
require_once 'Xin/Controller/Action.php';
class IndexController extends Xin_Controller_Action
{
// This will use default.phtml layout
public function indexAction() {
$this->view->my_fruit = "apple";
}
}
?>
application/views/scripts/index/index.phtml file
PHP Code:
<h2>Index Controller Content</h2>
application/views/scripts/layouts/default.phtml file
PHP Code:
<html>
<title>Default Layout</title>
<h1>HEADER</h1>
<body>
<h2>Fruit = <?= $this->my_fruit ?></h2>
<?= $this->content_for_layout ?>
</body>
<h1>FOOTER</h1>
</html>
application/views/scripts/layouts/customize.phtml file
PHP Code:
<html>
<title>Custom Layout</title>
<h1>HEADER</h1>
<body>
<h2>New Fruit = <?= $this->my_fruit ?></h2>
<?= $his->content_for_layout ?>
</body>
<h1>FOOTER</h1>
</html>
html/index.php file
PHP Code:
<?php
require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('../application/controllers');
?>
To use customize.phtml, your IndexController action index should be
Code:
public function indexAction() {
$this->_layout->setName('customize');
$this->view->my_fruit = 'apple';
}