diff --git a/Mustache.php b/Mustache.php index 0cef70c..d24b632 100644 --- a/Mustache.php +++ b/Mustache.php @@ -615,7 +615,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)))); } } diff --git a/examples/section_iterator_objects/SectionIteratorObjects.php b/examples/section_iterator_objects/SectionIteratorObjects.php new file mode 100644 index 0000000..fa5213c --- /dev/null +++ b/examples/section_iterator_objects/SectionIteratorObjects.php @@ -0,0 +1,40 @@ + '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]); + } +} \ No newline at end of file diff --git a/examples/section_iterator_objects/section_iterator_objects.mustache b/examples/section_iterator_objects/section_iterator_objects.mustache new file mode 100644 index 0000000..44dfce4 --- /dev/null +++ b/examples/section_iterator_objects/section_iterator_objects.mustache @@ -0,0 +1,5 @@ +* {{ start }} +{{# middle }} +* {{ item }} +{{/ middle }} +* {{ final }} \ No newline at end of file diff --git a/examples/section_iterator_objects/section_iterator_objects.txt b/examples/section_iterator_objects/section_iterator_objects.txt new file mode 100644 index 0000000..e6b2d7a --- /dev/null +++ b/examples/section_iterator_objects/section_iterator_objects.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. \ No newline at end of file diff --git a/examples/section_magic_objects/SectionMagicObjects.php b/examples/section_magic_objects/SectionMagicObjects.php new file mode 100644 index 0000000..ebb0031 --- /dev/null +++ b/examples/section_magic_objects/SectionMagicObjects.php @@ -0,0 +1,26 @@ + 'And it worked the second time.', + 'bar' => 'As well as the third.' + ); + + public function __get($key) { + return isset($this->_data[$key]) ? $this->_data[$key] : NULL; + } + + public function __isset($key) { + return isset($this->_data[$key]); + } +} \ No newline at end of file diff --git a/examples/section_magic_objects/section_magic_objects.mustache b/examples/section_magic_objects/section_magic_objects.mustache new file mode 100644 index 0000000..9119608 --- /dev/null +++ b/examples/section_magic_objects/section_magic_objects.mustache @@ -0,0 +1,6 @@ +* {{ start }} +{{# middle }} +* {{ foo }} +* {{ bar }} +{{/ middle }} +* {{ final }} \ No newline at end of file diff --git a/examples/section_magic_objects/section_magic_objects.txt b/examples/section_magic_objects/section_magic_objects.txt new file mode 100644 index 0000000..e6b2d7a --- /dev/null +++ b/examples/section_magic_objects/section_magic_objects.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. \ No newline at end of file diff --git a/examples/section_objects/SectionObjects.php b/examples/section_objects/SectionObjects.php new file mode 100644 index 0000000..41b7d84 --- /dev/null +++ b/examples/section_objects/SectionObjects.php @@ -0,0 +1,16 @@ +assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}')); + } + + public function testObjectWithGet() { + $beta = new Beta(); + $this->assertEquals('Foo', $beta->render('{{#foo}}{{name}}{{/foo}}')); + } + + public function testSectionObjectWithGet() { + $gamma = new Gamma(); + $this->assertEquals('Foo', $gamma->render('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}')); + } +} + +class Alpha extends Mustache { + public $foo; + + public function __construct() { + $this->foo = new StdClass(); + $this->foo->name = 'Foo'; + $this->foo->number = 1; + } +} + +class Beta extends Mustache { + protected $_data = array(); + + public function __construct() { + $this->_data['foo'] = new StdClass(); + $this->_data['foo']->name = 'Foo'; + $this->_data['foo']->number = 1; + } + + public function __isset($name) { + return array_key_exists($name, $this->_data); + } + + public function __get($name) { + return $this->_data[$name]; + } +} + +class Gamma extends Mustache { + public $bar; + + public function __construct() { + $this->bar = new Beta(); + } +} \ No newline at end of file diff --git a/test/MustacheTest.php b/test/MustacheTest.php index c05b15b..a877c03 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -262,26 +262,30 @@ class MustacheTest extends PHPUnit_Framework_TestCase { foreach ($children as $file) { if (!$file->isFile()) continue; - $filename = $file->getPathInfo(); + $filename = $file->getPathname(); $info = pathinfo($filename); - switch($info['extension']) { - case 'php': - $class = $info['filename']; - include_once($filename); - break; + if (isset($info['extension'])) { + switch($info['extension']) { + case 'php': + $class = $info['filename']; + include_once($filename); + break; - case 'mustache': - $template = file_get_contents($filename); - break; + case 'mustache': + $template = file_get_contents($filename); + break; - case 'txt': - $output = file_get_contents($filename); - break; + case 'txt': + $output = file_get_contents($filename); + break; + } } } - $ret[$example] = array($class, $template, $output); + if (!empty($class)) { + $ret[$example] = array($class, $template, $output); + } } $files->next();