From eeb5b245d13341f567c9b9dcebe2b2814083b4b4 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sat, 22 May 2010 18:34:24 -0400 Subject: [PATCH] Possible fix for #1 - ability to use partials with an arbitrary (non-Mustache) view class. --- Mustache.php | 48 ++----------------- .../PartialsWithViewClass.php | 19 ++++++++ .../partials_with_view_class.mustache | 2 + .../partials_with_view_class.txt | 3 ++ 4 files changed, 28 insertions(+), 44 deletions(-) create mode 100644 examples/partials_with_view_class/PartialsWithViewClass.php create mode 100644 examples/partials_with_view_class/partials_with_view_class.mustache create mode 100644 examples/partials_with_view_class/partials_with_view_class.txt diff --git a/Mustache.php b/Mustache.php index e15438a..19229af 100644 --- a/Mustache.php +++ b/Mustache.php @@ -424,10 +424,10 @@ class Mustache { * @return string */ protected function _renderPartial($tag_name, &$context) { - $view = new self($this->_getPartial($tag_name), $this->_flattenContext($context), $this->_partials); - $view->_otag = $this->_otag; - $view->_ctag = $this->_ctag; - return $view->render(); + $view = clone($this); + $view->_otag = '{{'; + $view->_ctag = '}}'; + return $view->render($this->_getPartial($tag_name)); } /** @@ -450,7 +450,6 @@ class Mustache { return ''; } - /** * Prepare a new context reference array. * @@ -470,45 +469,6 @@ 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_with_view_class/PartialsWithViewClass.php b/examples/partials_with_view_class/PartialsWithViewClass.php new file mode 100644 index 0000000..56e0d86 --- /dev/null +++ b/examples/partials_with_view_class/PartialsWithViewClass.php @@ -0,0 +1,19 @@ +name = 'ilmich'; + $view->data = array( + array('name' => 'federica', 'age' => 27, 'gender' => 'female'), + array('name' => 'marco', 'age' => 32, 'gender' => 'male'), + ); + + $partials = array( + 'children' => "{{#data}}{{name}} - {{age}} - {{gender}}\n{{/data}}", + ); + + parent::__construct($template, $view, $partials); + } +} \ No newline at end of file diff --git a/examples/partials_with_view_class/partials_with_view_class.mustache b/examples/partials_with_view_class/partials_with_view_class.mustache new file mode 100644 index 0000000..037e1b3 --- /dev/null +++ b/examples/partials_with_view_class/partials_with_view_class.mustache @@ -0,0 +1,2 @@ +Children of {{name}}: +{{>children}} \ No newline at end of file diff --git a/examples/partials_with_view_class/partials_with_view_class.txt b/examples/partials_with_view_class/partials_with_view_class.txt new file mode 100644 index 0000000..d967e15 --- /dev/null +++ b/examples/partials_with_view_class/partials_with_view_class.txt @@ -0,0 +1,3 @@ +Children of ilmich: +federica - 27 - female +marco - 32 - male