ソースを参照

Fixing #3, Transversable objects should be iterated over.

Jeremy Bush 15 年 前
コミット
c45550e9e5

+ 1 - 1
Mustache.php

@@ -597,7 +597,7 @@ class Mustache {
 	 * @return bool
 	 */
 	protected function _varIsIterable($var) {
-		return is_object($var) || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var))));
+		return $var instanceof Traversable || (is_array($var) && !array_diff_key($var, array_keys(array_keys($var))));
 	}
 }
 

+ 41 - 0
examples/sections_iterator_object/Sections_Iterator_Object.php

@@ -0,0 +1,41 @@
+<?php
+
+class Sections_Iterator_Object extends Mustache {
+	public $start = "It worked the first time.";
+
+	public function middle() {
+		return new Iterator_Object;
+	}
+
+	public $final = "Then, surprisingly, it worked the final time.";
+}
+
+class Iterator_Object implements Iterator
+{
+	protected $_position = 0;
+
+	protected $_data = array(
+		array('item' => 'And it worked the second time.'),
+		array('item' => 'As well as the third.'),
+	);
+
+	public function rewind() {
+		$this->_position = 0;
+	}
+
+	public function current() {
+		return $this->_data[$this->_position];
+	}
+
+	public function key() {
+		return $this->_position;
+	}
+
+	public function next() {
+		++$this->_position;
+	}
+
+	public function valid() {
+		return isset($this->_data[$this->_position]);
+	}
+}

+ 5 - 0
examples/sections_iterator_object/sections.mustache

@@ -0,0 +1,5 @@
+* {{ start }}
+{{# middle }}
+* {{ item }}
+{{/ middle }}
+* {{ final }}

+ 4 - 0
examples/sections_iterator_object/sections.txt

@@ -0,0 +1,4 @@
+* It worked the first time.
+* And it worked the second time.
+* As well as the third.
+* Then, surprisingly, it worked the final time.