diff --git a/Mustache.php b/Mustache.php index 8a4e6c8..e1f69cd 100644 --- a/Mustache.php +++ b/Mustache.php @@ -142,12 +142,16 @@ class Mustache { // regular section case '#': - if (is_array($val)) { + if ($this->varIsIterable($val)) { foreach ($val as $local_context) { $replace .= $this->_render($content, $this->getContext($context, $local_context)); } } else if ($val) { - $replace .= $content; + if (is_array($val) || is_object($val)) { + $replace .= $this->_render($content, $this->getContext($context, $val)); + } else { + $replace .= $content; + } } break; } @@ -387,6 +391,17 @@ class Mustache { } } + /** + * Check whether the given $var should be iterated (i.e. in a section context). + * + * @access protected + * @param mixed $var + * @return bool + */ + protected function varIsIterable($var) { + return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var)))); + } + /** * Prepare a string to be used in a regular expression. * diff --git a/examples/child_context/ChildContext.php b/examples/child_context/ChildContext.php new file mode 100644 index 0000000..b652356 --- /dev/null +++ b/examples/child_context/ChildContext.php @@ -0,0 +1,13 @@ + 'child works', + ); + + public $grandparent = array( + 'parent' => array( + 'child' => 'grandchild works', + ), + ); +} \ No newline at end of file diff --git a/examples/child_context/child_context.mustache b/examples/child_context/child_context.mustache new file mode 100644 index 0000000..e1f2ebc --- /dev/null +++ b/examples/child_context/child_context.mustache @@ -0,0 +1,2 @@ +