From 05767e3b74a67a9a204040939cb531752225da34 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 6 Apr 2010 01:11:27 -0400 Subject: [PATCH 1/7] Trailing whitespace cleanup. --- Mustache.php | 2 +- examples/complex/complex.php | 8 ++++---- examples/delimiters/Delimiters.php | 4 ++-- examples/double_section/DoubleSection.php | 2 +- examples/sections/Sections.php | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Mustache.php b/Mustache.php index d8d42f6..2133df5 100644 --- a/Mustache.php +++ b/Mustache.php @@ -24,7 +24,7 @@ class Mustache { // Override charset passed to htmlentities() and htmlspecialchars(). Defaults to UTF-8. protected $charset = 'UTF-8'; - + protected $tagRegEx; protected $template = ''; diff --git a/examples/complex/complex.php b/examples/complex/complex.php index ca32ed7..0631279 100644 --- a/examples/complex/complex.php +++ b/examples/complex/complex.php @@ -2,22 +2,22 @@ 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 isLink() { // Exploit the fact that the current iteration item is at the top of the context stack. return $this->getVariable('current', $this->context) != true; } - + 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 From bb4905dd1b2081b38fa63e6b8329858e20e94be6 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 20:35:46 -0400 Subject: [PATCH 2/7] Added known issue for missing child context in sections. --- README.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 0634a0f..5ee9dae 100644 --- a/README.markdown +++ b/README.markdown @@ -81,7 +81,10 @@ And render it: Known Issues ------------ - * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. + * Sections don't respect delimiter changes -- `delimiters` example currently fails with an + "unclosed section" exception. + * Sections don't have access to child context, i.e. `{{#foo}}{{bar}}{{/foo}}' fails if the context + is `array('foo' => array('bar' => 'baz'));` * Test coverage is incomplete. From 2fbc599c261746723e774ad8e0615ebf20a2cd47 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 21:07:47 -0400 Subject: [PATCH 3/7] fixing markup typo in README --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 5ee9dae..e4334b5 100644 --- a/README.markdown +++ b/README.markdown @@ -83,7 +83,7 @@ Known Issues * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. - * Sections don't have access to child context, i.e. `{{#foo}}{{bar}}{{/foo}}' fails if the context + * Sections don't have access to child context, i.e. `{{#foo}}{{bar}}{{/foo}}` fails if the context is `array('foo' => array('bar' => 'baz'));` * Test coverage is incomplete. From 0450bcfb464b8b87cd37371f98cbcb398d452f07 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 21:10:55 -0400 Subject: [PATCH 4/7] Removing traversable mustache class. This was moved into pragma-dot-notation feature branch. --- TraversableMustache.php | 51 ----------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 TraversableMustache.php diff --git a/TraversableMustache.php b/TraversableMustache.php deleted file mode 100644 index 79dfc8e..0000000 --- a/TraversableMustache.php +++ /dev/null @@ -1,51 +0,0 @@ - array( - * 'three' => 'wheee!' - * ) - * ); - * - * protected $template = '{{one.two.three}}'; - * } - * $foo = new Foo; - * print $foo; - * @endcode - * - * (The above code prints 'wheee!') - * - * @extends Mustache - */ -class TraversableMustache extends Mustache { - - /** - * Override default getVariable method to allow object traversal via dots. - * This might be cool. Also, might be heinous. - * - * @access protected - * @param string $tag_name - * @param array &$context - * @return string - */ - protected function getVariable($tag_name, &$context) { - $chunks = explode('.', $tag_name); - $first = array_shift($chunks); - - $ret = parent::getVariable($first, $context); - while ($next = array_shift($chunks)) { - $c = array($ret); - $ret = parent::getVariable($next, $c); - } - - return $ret; - } -} \ No newline at end of file From 668b538669eb5716c42bad1f04bdfc572b03f9c2 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 21:40:02 -0400 Subject: [PATCH 5/7] 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. * From 5a819a33da26e79e7d4fe05368327c3d58714c05 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 21:49:54 -0400 Subject: [PATCH 6/7] Fix for missing child context in sections. --- Mustache.php | 6 +++++- examples/child_context/ChildContext.php | 13 +++++++++++++ examples/child_context/child_context.mustache | 2 ++ examples/child_context/child_context.txt | 2 ++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 examples/child_context/ChildContext.php create mode 100644 examples/child_context/child_context.mustache create mode 100644 examples/child_context/child_context.txt diff --git a/Mustache.php b/Mustache.php index 5cd1137..e1f69cd 100644 --- a/Mustache.php +++ b/Mustache.php @@ -147,7 +147,11 @@ class Mustache { $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; } 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 From 5832dd5b5d48a020192ecc6ae1bafd4dd89cf8e2 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 26 Apr 2010 21:58:16 -0400 Subject: [PATCH 7/7] Updating README to reflect child context fix. --- README.markdown | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.markdown b/README.markdown index e4334b5..ab27846 100644 --- a/README.markdown +++ b/README.markdown @@ -83,8 +83,6 @@ Known Issues * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. - * Sections don't have access to child context, i.e. `{{#foo}}{{bar}}{{/foo}}` fails if the context - is `array('foo' => array('bar' => 'baz'));` * Test coverage is incomplete.