From 2c3984a604c91fd9cac22afa415161fc6330eb13 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 16 May 2010 15:04:22 -0400 Subject: [PATCH] Fix for #1 - incorrect context for partials. Partials now receive a flattened version of the parent context, including a cloned Mustache instance (if the root context is a Mustache instance). There may be issues with cloning, for example: if a pragma is enabled in the parent template, partials might also inherit the pragma. Added (basic) test case for partials. --- Mustache.php | 45 +++++++++++++++++++++++++++-- examples/partials/Partials.php | 13 +++++++++ examples/partials/partials.mustache | 2 ++ examples/partials/partials.txt | 3 ++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 examples/partials/Partials.php create mode 100644 examples/partials/partials.mustache create mode 100644 examples/partials/partials.txt diff --git a/Mustache.php b/Mustache.php index 85dae70..44389f4 100644 --- a/Mustache.php +++ b/Mustache.php @@ -424,7 +424,7 @@ class Mustache { * @return string */ protected function _renderPartial($tag_name, &$context) { - $view = new self($this->_getPartial($tag_name), $context, $this->_partials); + $view = new self($this->_getPartial($tag_name), $this->_flattenContext($context), $this->_partials); $view->_otag = $this->_otag; $view->_ctag = $this->_ctag; return $view->render(); @@ -458,8 +458,8 @@ class Mustache { * * @access protected * @param array $context - * @param mixed $local_context - * @return void + * @param array $local_context + * @return array */ protected function _getContext(&$context, &$local_context) { $ret = array(); @@ -470,6 +470,45 @@ class Mustache { return $ret; } + + /** + * Prepare a new (flattened) context. + * + * This is used to create a view object or array for rendering partials. + * + * @access protected + * @param array &$context + * @return array + * @throws MustacheException + */ + protected function _flattenContext(&$context) { + $keys = array_keys($context); + $first = $context[$keys[0]]; + + if ($first instanceof Mustache) { + $ret = clone $first; + unset($keys[0]); + + foreach ($keys as $name) { + foreach ($context[$name] as $key => $val) { + $ret->$key =& $val; + } + } + } else if (is_array($first)) { + $ret = array(); + + foreach ($keys as $name) { + foreach ($context[$name] as $key => $val) { + $ret[$key] =& $val; + } + } + } else { + throw new MustacheException('Unknown root context type.'); + } + + return $ret; + } + /** * Get a variable from the context array. * diff --git a/examples/partials/Partials.php b/examples/partials/Partials.php new file mode 100644 index 0000000..093257b --- /dev/null +++ b/examples/partials/Partials.php @@ -0,0 +1,13 @@ + 'federica', 'age' => 27, 'gender' => 'female'), + array('name' => 'marco', 'age' => 32, 'gender' => 'male'), + ); + + protected $_partials = array( + 'children' => "{{#data}}{{name}} - {{age}} - {{gender}}\n{{/data}}", + ); +} \ No newline at end of file diff --git a/examples/partials/partials.mustache b/examples/partials/partials.mustache new file mode 100644 index 0000000..037e1b3 --- /dev/null +++ b/examples/partials/partials.mustache @@ -0,0 +1,2 @@ +Children of {{name}}: +{{>children}} \ No newline at end of file diff --git a/examples/partials/partials.txt b/examples/partials/partials.txt new file mode 100644 index 0000000..d967e15 --- /dev/null +++ b/examples/partials/partials.txt @@ -0,0 +1,3 @@ +Children of ilmich: +federica - 27 - female +marco - 32 - male