瀏覽代碼

Prevent iteration over associative arrays.

Justin Hileman 15 年之前
父節點
當前提交
668b538669
共有 1 個文件被更改,包括 12 次插入1 次删除
  1. 12 1
      Mustache.php

+ 12 - 1
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.
 	 *