From 668b538669eb5716c42bad1f04bdfc572b03f9c2 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 21:40:02 -0400 Subject: [PATCH] Prevent iteration over associative arrays. --- Mustache.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index 8a4e6c8..5cd1137 100644 --- a/Mustache.php +++ b/Mustache.php @@ -142,7 +142,7 @@ 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)); } @@ -387,6 +387,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. *