Skip to content

Instantly share code, notes, and snippets.

@davidreuss
Created October 4, 2009 19:14
Show Gist options
  • Save davidreuss/201564 to your computer and use it in GitHub Desktop.
Save davidreuss/201564 to your computer and use it in GitHub Desktop.
<?php
class Zend_View_Helper_AddLayout {
protected $_plugin;
public function __construct() {
$this->_getPlugin();
}
protected function _getPlugin() {
$front = Zend_Controller_Front::getInstance();
$pluginClass = 'My_Controller_Plugin_NestedLayout';
$plugin = $front->getPlugin($pluginClass);
if ($plugin) {
$this->_plugin = $plugin;
return;
}
$this->_plugin = new $pluginClass;
Zend_Controller_Front::getInstance()->registerPlugin($this->_plugin);
}
public function addLayout($name) {
$this->_plugin->addLayout($name);
}
}
?>
<?php
class My_Controller_Plugin_NestedLayout extends Zend_Controller_Plugin_Abstract {
protected $_layouts = array();
protected $_layoutsAtPostDispatch = array();
public function addLayout($layout) {
$this->_layouts = array_merge(
$this->_layouts,
array_diff((array) $layout, $this->_layouts)
);
}
protected function _renderLayouts() {
$mvcLayout = Zend_Layout::getMvcInstance();
$this->_layoutsAtPostDispatch = array_reverse($this->_layouts);
$layout = clone $mvcLayout;
foreach ($this->_layouts as $layoutName) {
$layout->setLayout($layoutName);
$layout->{$mvcLayout->getContentKey()} = $this->getResponse()->getBody();
$this->getResponse()->setBody($layout->render());
}
$newLayouts = array_diff(
$this->_layouts,
$this->_layoutsAtPostDispatch
);
if (count($newLayouts) > 0) {
$this->_layouts = array_diff(
$this->_layouts,
$this->_layoutsAtPostDispatch
);
$this->_renderLayouts();
}
}
public function postDispatch() {
$this->_renderLayouts();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment