Skip to content

Instantly share code, notes, and snippets.

@davidreuss
Created October 4, 2009 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidreuss/201465 to your computer and use it in GitHub Desktop.
Save davidreuss/201465 to your computer and use it in GitHub Desktop.
Patch to get nested layouts with Zend_Layout
diff --git a/Layout.php b/Layout.php
index 46de129..bfd5ded 100644
--- a/Layout.php
+++ b/Layout.php
@@ -29,6 +29,25 @@
*/
class Zend_Layout
{
+
+ /**
+ * Current layout name, being rendered
+ * @var string
+ */
+ protected $_current = null;
+
+ /**
+ * Internal stack to control layout nesting
+ * @var array
+ */
+ protected $_stack = array();
+
+ /**
+ * Holds information about layouts and their parents
+ * @var array
+ */
+ protected $_parents = array();
+
/**
* Placeholder container for layout variables
* @var Zend_View_Helper_Placeholder_Container
@@ -756,6 +775,17 @@ class Zend_Layout
}
/**
+ * Extend current layout with named layout
+ *
+ * @param string $name The name of the layout
+ * @return Zend_Layout
+ */
+ public function extend($name) {
+ $this->_parents[$this->_current] = $name;
+ return $this;
+ }
+
+ /**
* Render layout
*
* Sets internal script path as last path on script path stack, assigns
@@ -773,9 +803,13 @@ class Zend_Layout
$name = $this->getLayout();
}
+ $this->_current = $name;
+ $this->_parents[$name] = null;
+ $viewName = $name;
+
if ($this->inflectorEnabled() && (null !== ($inflector = $this->getInflector())))
{
- $name = $this->_inflector->filter(array('script' => $name));
+ $viewName = $this->_inflector->filter(array('script' => $name));
}
$view = $this->getView();
@@ -790,6 +824,16 @@ class Zend_Layout
$view->addBasePath($path, $this->_viewBasePrefix);
}
- return $view->render($name);
+ $content = $view->render($viewName);
+
+ // Decorate with parent layouts if any is registered
+ if ($this->_parents[$name]) {
+ $this->_stack[] = $this->content;
+ $this->content = $content;
+ $content = $this->render($this->_parents[$name]);
+ $this->content = array_pop($this->_stack);
+ }
+
+ return $content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment