From 77f9a37b9ecb216753c0fbd7df17ad8352226f47 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:12:04 -0700 Subject: [PATCH 01/14] Moving $orig_tags to render --- Mustache.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Mustache.php b/Mustache.php index 19e9cbb..b6b08e1 100644 --- a/Mustache.php +++ b/Mustache.php @@ -175,6 +175,9 @@ class Mustache { public function render($template = null, $view = null, $partials = null) { if ($template === null) $template = $this->_template; if ($partials !== null) $this->_partials = $partials; + + $otag_orig = $this->_otag; + $ctag_orig = $this->_ctag; if ($view) { $this->_context = array($view); @@ -183,7 +186,12 @@ class Mustache { } $template = $this->_renderPragmas($template); - return $this->_renderTemplate($template, $this->_context); + $template = $this->_renderTemplate($template, $this->_context); + + $this->_otag = $otag_orig; + $this->_ctag = $ctag_orig; + + return $template; } /** @@ -475,9 +483,6 @@ class Mustache { return $template; } - $otag_orig = $this->_otag; - $ctag_orig = $this->_ctag; - $first = true; $this->_tagRegEx = $this->_prepareTagRegEx($this->_otag, $this->_ctag, true); @@ -517,9 +522,6 @@ class Mustache { } } - $this->_otag = $otag_orig; - $this->_ctag = $ctag_orig; - return $html . $template; } From 5e7f0ea4bded0f3c52c6959632de2c1a9f73e680 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:12:47 -0700 Subject: [PATCH 02/14] Relocating _renderTags($before) since _renderTags now has the ability to modify delimiters --- Mustache.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index b6b08e1..d0c0da6 100644 --- a/Mustache.php +++ b/Mustache.php @@ -219,8 +219,11 @@ class Mustache { * @return string Rendered Mustache template. */ protected function _renderTemplate($template) { + if ($section = $this->_findSection($template)) { list($before, $type, $tag_name, $content, $after) = $section; + + $rendered_before = $this->_renderTags($before); $renderedContent = ''; $val = $this->_getVariable($tag_name); @@ -252,7 +255,7 @@ class Mustache { break; } - return $this->_renderTags($before) . $renderedContent . $this->_renderTemplate($after); + return $rendered_before . $renderedContent . $this->_renderTemplate($after); } return $this->_renderTags($template); From eff36b5540ae0b13b4f99d601b22240881a42395 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 03:26:27 -0400 Subject: [PATCH 03/14] Coding style? Conventions? Consistency? Who needs that?. Mebbe I should get some sleep :) --- Mustache.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Mustache.php b/Mustache.php index d0c0da6..779ff34 100644 --- a/Mustache.php +++ b/Mustache.php @@ -225,13 +225,13 @@ class Mustache { $rendered_before = $this->_renderTags($before); - $renderedContent = ''; + $rendered_content = ''; $val = $this->_getVariable($tag_name); switch($type) { // inverted section case '^': if (empty($val)) { - $renderedContent = $this->_renderTemplate($content); + $rendered_content = $this->_renderTemplate($content); } break; @@ -240,22 +240,22 @@ class Mustache { if ($this->_varIsIterable($val)) { foreach ($val as $local_context) { $this->_pushContext($local_context); - $renderedContent .= $this->_renderTemplate($content); + $rendered_content .= $this->_renderTemplate($content); $this->_popContext(); } } else if ($val) { if (is_array($val) || is_object($val)) { $this->_pushContext($val); - $renderedContent = $this->_renderTemplate($content); + $rendered_content = $this->_renderTemplate($content); $this->_popContext(); } else { - $renderedContent = $this->_renderTemplate($content); + $rendered_content = $this->_renderTemplate($content); } } break; } - return $rendered_before . $renderedContent . $this->_renderTemplate($after); + return $rendered_before . $rendered_content . $this->_renderTemplate($after); } return $this->_renderTags($template); From a7cf3e7309dbbdedf58bcdaca9a9bd5ba7cc6cfd Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:31:36 -0700 Subject: [PATCH 04/14] Adding test for delimiters --- test/MustacheTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index 70622b4..aded7bc 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -402,6 +402,15 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals('success', $m->render('{{=<% %>=}}<% result %>')); } + /** + * @group delimiters + */ + public function testStickyDelimiters() { + $m = new Mustache(null, array('result' => 'FAIL')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{#result}}FAIL{{/result}}{{result}}[[={{ }}=]]')); + } + /** * @group sections * @dataProvider poorlyNestedSections From a2f3662b6b897651429c2d9a50ab7b65a5f5446d Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 03:54:15 -0400 Subject: [PATCH 05/14] update failing test case, test for sticky delims between renders. --- test/MustacheTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index aded7bc..d62d648 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -36,7 +36,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase { const TEST_CLASS = 'Mustache'; protected $knownIssues = array( - 'Delimiters' => "Known issue: sections don't respect delimiter changes", + // Just the whitespace ones... ); /** @@ -408,7 +408,8 @@ class MustacheTest extends PHPUnit_Framework_TestCase { public function testStickyDelimiters() { $m = new Mustache(null, array('result' => 'FAIL')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); - $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{#result}}FAIL{{/result}}{{result}}[[={{ }}=]]')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]]')); + $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]][[={{ }}=]]')); } /** From 25070141a945c009180d49912eb020adeda9c941 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 00:55:31 -0700 Subject: [PATCH 06/14] Fixing broken test --- test/MustacheTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index aded7bc..8245e5d 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -408,7 +408,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase { public function testStickyDelimiters() { $m = new Mustache(null, array('result' => 'FAIL')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); - $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{#result}}FAIL{{/result}}{{result}}[[={{ }}=]]')); + $this->assertEquals('{{#result}}{{/result}}', $m->render('{{=[[ ]]=}}{{#result}}{{/result}}[[={{ }}=]]')); } /** From a16d9ed51c1c234b78cdfc7144171feee47c7a3b Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 04:00:13 -0400 Subject: [PATCH 07/14] add a test that changes delimiters all over the place... inside and outside sections. --- README.markdown | 2 -- test/MustacheTest.php | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.markdown b/README.markdown index c3f6831..2c13ab3 100644 --- a/README.markdown +++ b/README.markdown @@ -81,8 +81,6 @@ And render it: Known Issues ------------ - * Things get weird when you change delimiters inside a section -- `delimiters` example currently fails with an - "unclosed section" exception. * As of v1.1.2, there are a couple of whitespace bugs around section tags... Despite these failing tests, this version is actually *closer* to correct than previous releases. diff --git a/test/MustacheTest.php b/test/MustacheTest.php index d62d648..7edf232 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -410,6 +410,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}{{ result }}[[={{ }}=]]')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]]')); $this->assertEquals('{{ result }}', $m->render('{{=[[ ]]=}}[[#result]]{{ result }}[[/result]][[={{ }}=]]')); + $this->assertEquals('{{ result }}', $m->render('{{#result}}{{=[[ ]]=}}{{ result }}[[/result]][[^result]][[={{ }}=]][[ result ]]{{/result}}')); } /** From 1abf854af3ca3bb6b7943a32ed7d5dc69ddaefa3 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 04:00:49 -0400 Subject: [PATCH 08/14] Make section rendering delimiter-chage-aware. Fixes longstanding section/delimiter bug. --- Mustache.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index 779ff34..ef3af03 100644 --- a/Mustache.php +++ b/Mustache.php @@ -271,7 +271,7 @@ class Mustache { */ protected function _prepareSectionRegEx($otag, $ctag) { return sprintf( - '/(?:(?<=\\n)[ \\t]*)?%s(?P[%s])(?P.+?)%s\\n?/s', + '/(?:(?<=\\n)[ \\t]*)?%s(?:(?P[%s])(?P.+?)|=(?P.*?)=)%s\\n?/s', preg_quote($otag, '/'), self::SECTION_TYPES, preg_quote($ctag, '/') @@ -297,6 +297,12 @@ class Mustache { $section_stack = array(); $matches = array(); while (preg_match($regEx, $template, $matches, PREG_OFFSET_CAPTURE, $search_offset)) { + if (isset($matches['delims'][0])) { + list($otag, $ctag) = explode(' ', $matches['delims'][0]); + $regEx = $this->_prepareSectionRegEx($otag, $ctag); + $search_offset = $matches[0][1] + strlen($matches[0][0]); + continue; + } $match = $matches[0][0]; $offset = $matches[0][1]; From 527bca37f35cc95c7a7c58cb56dce987b45e4209 Mon Sep 17 00:00:00 2001 From: KevBurnsJr Date: Mon, 13 Jun 2011 01:01:33 -0700 Subject: [PATCH 09/14] Fixing testStickyDelimiters --- Mustache.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Mustache.php b/Mustache.php index d0c0da6..3a64e6d 100644 --- a/Mustache.php +++ b/Mustache.php @@ -219,7 +219,6 @@ class Mustache { * @return string Rendered Mustache template. */ protected function _renderTemplate($template) { - if ($section = $this->_findSection($template)) { list($before, $type, $tag_name, $content, $after) = $section; @@ -271,7 +270,7 @@ class Mustache { */ protected function _prepareSectionRegEx($otag, $ctag) { return sprintf( - '/(?:(?<=\\n)[ \\t]*)?%s(?P[%s])(?P.+?)%s\\n?/s', + '/(?:(?<=\\n)[ \\t]*)?%s(?:(?P[%s])(?P.+?)|=(?P.*?)=)%s\\n?/s', preg_quote($otag, '/'), self::SECTION_TYPES, preg_quote($ctag, '/') @@ -297,11 +296,18 @@ class Mustache { $section_stack = array(); $matches = array(); while (preg_match($regEx, $template, $matches, PREG_OFFSET_CAPTURE, $search_offset)) { - + $match = $matches[0][0]; $offset = $matches[0][1]; $type = $matches['type'][0]; $tag_name = trim($matches['tag_name'][0]); + + if (isset($matches['delims'][0])) { + list($otag, $ctag) = explode(' ', $matches['delims'][0]); + $regEx = $this->_prepareSectionRegEx($otag, $ctag); + $search_offset = $offset + strlen($match); + continue; + } $search_offset = $offset + strlen($match); From 3a8b4b0ec79c7af91d3c43f56831a2468a03a346 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 04:26:35 -0400 Subject: [PATCH 10/14] Update manpage links in README Closes #41 Thanks ddrake --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 2c13ab3..f2dbe39 100644 --- a/README.markdown +++ b/README.markdown @@ -89,4 +89,4 @@ See Also -------- * [Readme for the Ruby Mustache implementation](http://github.com/defunkt/mustache/blob/master/README.md). - * [mustache(1)](http://defunkt.github.com/mustache/mustache.1.html) and [mustache(5)](http://defunkt.github.com/mustache/mustache.5.html) man pages. + * [mustache(1)](http://mustache.github.com/mustache.1.html) and [mustache(5)](http://mustache.github.com/mustache.5.html) man pages. From a086a21b5fc668b9a01b6f52c7578cfc713ed48d Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 04:33:03 -0400 Subject: [PATCH 11/14] clarifying which version tag --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index f2dbe39..830df90 100644 --- a/README.markdown +++ b/README.markdown @@ -81,7 +81,7 @@ And render it: Known Issues ------------ - * As of v1.1.2, there are a couple of whitespace bugs around section tags... Despite these failing tests, this + * As of Mustache spec v1.1.2, there are a couple of whitespace bugs around section tags... Despite these failing tests, this version is actually *closer* to correct than previous releases. From 28b5a457cef50299fd52c7ce038e7629ce321e54 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 04:38:24 -0400 Subject: [PATCH 12/14] fix merge conflict. --- Mustache.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Mustache.php b/Mustache.php index 3f7a572..5b7b57b 100644 --- a/Mustache.php +++ b/Mustache.php @@ -307,13 +307,6 @@ class Mustache { $offset = $matches[0][1]; $type = $matches['type'][0]; $tag_name = trim($matches['tag_name'][0]); - - if (isset($matches['delims'][0])) { - list($otag, $ctag) = explode(' ', $matches['delims'][0]); - $regEx = $this->_prepareSectionRegEx($otag, $ctag); - $search_offset = $offset + strlen($match); - continue; - } $search_offset = $offset + strlen($match); From b7a12665ea003b3f9ffd3c43a02d3136b48e9b71 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 15:12:43 -0400 Subject: [PATCH 13/14] Add a VERSION constant. closes #45 --- Mustache.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Mustache.php b/Mustache.php index 5b7b57b..ed27c59 100644 --- a/Mustache.php +++ b/Mustache.php @@ -14,6 +14,8 @@ */ class Mustache { + const VERSION = '0.7.1-dev'; + /** * Should this Mustache throw exceptions when it finds unexpected tags? * @@ -175,7 +177,7 @@ class Mustache { public function render($template = null, $view = null, $partials = null) { if ($template === null) $template = $this->_template; if ($partials !== null) $this->_partials = $partials; - + $otag_orig = $this->_otag; $ctag_orig = $this->_ctag; @@ -190,7 +192,7 @@ class Mustache { $this->_otag = $otag_orig; $this->_ctag = $ctag_orig; - + return $template; } @@ -221,7 +223,7 @@ class Mustache { protected function _renderTemplate($template) { if ($section = $this->_findSection($template)) { list($before, $type, $tag_name, $content, $after) = $section; - + $rendered_before = $this->_renderTags($before); $rendered_content = ''; From fbcea210e3fcdb54416d9749cfb3a44d249b0804 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 13 Jun 2011 15:14:29 -0400 Subject: [PATCH 14/14] Version bump --- Mustache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mustache.php b/Mustache.php index ed27c59..33c9288 100644 --- a/Mustache.php +++ b/Mustache.php @@ -14,7 +14,7 @@ */ class Mustache { - const VERSION = '0.7.1-dev'; + const VERSION = '0.7.1'; /** * Should this Mustache throw exceptions when it finds unexpected tags?