diff --git a/Mustache.php b/Mustache.php index 88adef3..0aaf896 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; } @@ -411,6 +415,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/README.markdown b/README.markdown index a9685e1..61cd5bb 100644 --- a/README.markdown +++ b/README.markdown @@ -81,8 +81,10 @@ And render it: Known Issues ------------ - * There's no way to toggle a pragma other than checking out the feature branch for each pragma... Need a clean way to do this. - * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. + * There's no way to toggle a pragma other than checking out the feature branch for each pragma... + Need a clean way to do this. + * Sections don't respect delimiter changes -- `delimiters` example currently fails with an + "unclosed section" exception. * Test coverage is incomplete. 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 @@ +

{{#parent}}{{child}}{{/parent}}

+

{{#grandparent}}{{#parent}}{{child}}{{/parent}}{{/grandparent}}

\ No newline at end of file diff --git a/examples/child_context/child_context.txt b/examples/child_context/child_context.txt new file mode 100644 index 0000000..cfb76bf --- /dev/null +++ b/examples/child_context/child_context.txt @@ -0,0 +1,2 @@ +

child works

+

grandchild works

\ No newline at end of file diff --git a/examples/complex/complex.php b/examples/complex/complex.php index ea55418..32b0917 100644 --- a/examples/complex/complex.php +++ b/examples/complex/complex.php @@ -2,17 +2,17 @@ class Complex extends Mustache { public $header = 'Colors'; - + public $item = array( array('name' => 'red', 'current' => true, 'url' => '#Red'), array('name' => 'green', 'current' => false, 'url' => '#Green'), array('name' => 'blue', 'current' => false, 'url' => '#Blue'), ); - + public function notEmpty() { return !($this->isEmpty()); } - + public function isEmpty() { return count($this->item) === 0; } diff --git a/examples/delimiters/Delimiters.php b/examples/delimiters/Delimiters.php index 74ab583..be372fa 100644 --- a/examples/delimiters/Delimiters.php +++ b/examples/delimiters/Delimiters.php @@ -2,13 +2,13 @@ class Delimiters extends Mustache { public $start = "It worked the first time."; - + public function middle() { return array( array('item' => "And it worked the second time."), array('item' => "As well as the third."), ); } - + public $final = "Then, surprisingly, it worked the final time."; } \ No newline at end of file diff --git a/examples/double_section/DoubleSection.php b/examples/double_section/DoubleSection.php index 8aa1ac6..f9d3dbb 100644 --- a/examples/double_section/DoubleSection.php +++ b/examples/double_section/DoubleSection.php @@ -4,6 +4,6 @@ class DoubleSection extends Mustache { public function t() { return true; } - + public $two = "second"; } \ No newline at end of file diff --git a/examples/sections/Sections.php b/examples/sections/Sections.php index baa61d7..fb78354 100644 --- a/examples/sections/Sections.php +++ b/examples/sections/Sections.php @@ -2,13 +2,13 @@ class Sections extends Mustache { public $start = "It worked the first time."; - + public function middle() { return array( array('item' => "And it worked the second time."), array('item' => "As well as the third."), ); } - + public $final = "Then, surprisingly, it worked the final time."; } \ No newline at end of file