From 2cfc9ba82772d0c26d583ec842c0dfb982cbe350 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Thu, 17 Feb 2011 09:47:29 -0500 Subject: [PATCH 1/5] Update to current spec HEAD. This makes a lot of tests fail :) --- test/spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec b/test/spec index cf87c2f..fc53f5e 160000 --- a/test/spec +++ b/test/spec @@ -1 +1 @@ -Subproject commit cf87c2f067895f1a793454fc0112a6ad7ffe222f +Subproject commit fc53f5e7dccd0eeee9b9b7c9147b9773d5dc0889 From 5130ff2cbec33a2ee74abee352c70a797b405196 Mon Sep 17 00:00:00 2001 From: Luke Morton Date: Thu, 17 Feb 2011 21:19:13 +0000 Subject: [PATCH 2/5] Fixes #28 --- Mustache.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Mustache.php b/Mustache.php index 34c5e15..47ce1c6 100644 --- a/Mustache.php +++ b/Mustache.php @@ -615,8 +615,10 @@ class Mustache { */ protected function _renderPartial($tag_name, $whitespace = '') { $view = clone($this); - - return $whitespace . preg_replace('/\n(?!$)/s', "\n" . $whitespace, $view->render($this->_getPartial($tag_name))); + + $partial = $whitespace . preg_replace('/\n(?!$)/s', "\n" . $whitespace, $this->_getPartial($tag_name)); + + return $view->render($partial); } /** From 9640915fb7f15640dc2ddf9dbbcbb18b164be104 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 1 Mar 2011 08:20:40 -0500 Subject: [PATCH 3/5] Update spec to v1.0.0. Tests break. --- test/spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec b/test/spec index fc53f5e..6ae3df4 160000 --- a/test/spec +++ b/test/spec @@ -1 +1 @@ -Subproject commit fc53f5e7dccd0eeee9b9b7c9147b9773d5dc0889 +Subproject commit 6ae3df4325221b42cf744dc7f571e3365339a761 From fd3875a4ac8dbc12d68a55b7a3c89c1363e6fea9 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sat, 5 Mar 2011 11:16:12 -0500 Subject: [PATCH 4/5] Update spec to v1.1.0 -- dot notation --- test/spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec b/test/spec index 6ae3df4..3383fa6 160000 --- a/test/spec +++ b/test/spec @@ -1 +1 @@ -Subproject commit 6ae3df4325221b42cf744dc7f571e3365339a761 +Subproject commit 3383fa66e808a07fde1c291aa16a588d0a1a2a6d From 25253b3c00b2912b9aeb619cc4a0f46c56600cf7 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sat, 5 Mar 2011 11:49:43 -0500 Subject: [PATCH 5/5] DOT-NOTATION and IMPLICIT-ITERATOR are now part of core spec. These pragmas have proved useful enough that they're no longer considered pragmas. Enable them by default. Update tests accordingly. This is a (very) backwards incompatible change, so it won't be released until v0.6.0. --- Mustache.php | 60 ++----------- examples/dot_notation/dot_notation.mustache | 1 - .../implicit_iterator.mustache | 1 - test/MustachePragmaDotNotationTest.php | 63 -------------- test/MustachePragmaImplicitIteratorTest.php | 87 ------------------- test/MustachePragmaTest.php | 16 ++-- test/MustacheTest.php | 4 +- 7 files changed, 15 insertions(+), 217 deletions(-) delete mode 100644 test/MustachePragmaDotNotationTest.php delete mode 100644 test/MustachePragmaImplicitIteratorTest.php diff --git a/Mustache.php b/Mustache.php index f50d124..4455b1e 100644 --- a/Mustache.php +++ b/Mustache.php @@ -38,38 +38,6 @@ class Mustache { * will change in the future. */ - /** - * The {{%DOT-NOTATION}} pragma allows context traversal via dots. Given the following context: - * - * $context = array('foo' => array('bar' => array('baz' => 'qux'))); - * - * One could access nested properties using dot notation: - * - * {{%DOT-NOTATION}}{{foo.bar.baz}} - * - * Which would render as `qux`. - */ - const PRAGMA_DOT_NOTATION = 'DOT-NOTATION'; - - /** - * The {{%IMPLICIT-ITERATOR}} pragma allows access to non-associative array data in an - * iterable section: - * - * $context = array('items' => array('foo', 'bar', 'baz')); - * - * With this template: - * - * {{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}} - * - * Would render as `foobarbaz`. - * - * {{%IMPLICIT-ITERATOR}} accepts an optional 'iterator' argument which allows implicit - * iterator tags other than {{.}} ... - * - * {{%IMPLICIT-ITERATOR iterator=i}}{{#items}}{{i}}{{/items}} - */ - const PRAGMA_IMPLICIT_ITERATOR = 'IMPLICIT-ITERATOR'; - /** * The {{%UNESCAPED}} pragma swaps the meaning of the {{normal}} and {{{unescaped}}} * Mustache tags. That is, once this pragma is activated the {{normal}} tag will not be @@ -99,8 +67,6 @@ class Mustache { protected $_pragmas = array(); protected $_pragmasImplemented = array( - self::PRAGMA_DOT_NOTATION, - self::PRAGMA_IMPLICIT_ITERATOR, self::PRAGMA_UNESCAPED ); @@ -123,9 +89,7 @@ class Mustache { * * // an array of pragmas to enable * 'pragmas' => array( - * Mustache::PRAGMA_UNESCAPED, - * Mustache::PRAGMA_DOT_NOTATION, - * Mustache::PRAGMA_IMPLICIT_ITERATOR + * Mustache::PRAGMA_UNESCAPED * ), * ); * @@ -275,24 +239,8 @@ class Mustache { // regular section case '#': if ($this->_varIsIterable($val)) { - if ($this->_hasPragma(self::PRAGMA_IMPLICIT_ITERATOR)) { - if ($opt = $this->_getPragmaOptions(self::PRAGMA_IMPLICIT_ITERATOR)) { - $iterator = $opt['iterator']; - } else { - $iterator = '.'; - } - } else { - $iterator = false; - } - foreach ($val as $local_context) { - - if ($iterator) { - $iterator_context = array($iterator => $local_context); - $this->_pushContext($iterator_context); - } else { - $this->_pushContext($local_context); - } + $this->_pushContext($local_context); $replace .= $this->_renderTemplate($content); $this->_popContext(); } @@ -737,7 +685,9 @@ class Mustache { * @return string */ protected function _getVariable($tag_name) { - if ($tag_name != '.' && strpos($tag_name, '.') !== false && $this->_hasPragma(self::PRAGMA_DOT_NOTATION)) { + if ($tag_name === '.') { + return $this->_context[0]; + } else if (strpos($tag_name, '.') !== false) { $chunks = explode('.', $tag_name); $first = array_shift($chunks); diff --git a/examples/dot_notation/dot_notation.mustache b/examples/dot_notation/dot_notation.mustache index 4831386..0566867 100644 --- a/examples/dot_notation/dot_notation.mustache +++ b/examples/dot_notation/dot_notation.mustache @@ -1,4 +1,3 @@ -{{%DOT-NOTATION}} * {{person.name.first}} {{person.name.last}} * {{person.age}} * {{person.hometown.city}}, {{person.hometown.state}} diff --git a/examples/implicit_iterator/implicit_iterator.mustache b/examples/implicit_iterator/implicit_iterator.mustache index 94b82e1..b8d58ff 100644 --- a/examples/implicit_iterator/implicit_iterator.mustache +++ b/examples/implicit_iterator/implicit_iterator.mustache @@ -1,4 +1,3 @@ -{{%IMPLICIT-ITERATOR}} {{#data}} * {{.}} {{/data}} \ No newline at end of file diff --git a/test/MustachePragmaDotNotationTest.php b/test/MustachePragmaDotNotationTest.php deleted file mode 100644 index 56f6f11..0000000 --- a/test/MustachePragmaDotNotationTest.php +++ /dev/null @@ -1,63 +0,0 @@ - array('bar' => 'this worked'))); - - $this->assertEquals($m->render('{{foo.bar}}'), '', - 'Dot notation not enabled, variable should have been replaced with nothing'); - $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar}}'), 'this worked', - 'Dot notation enabled, variable should have been replaced by "this worked"'); - } - - public function testDeepTraversal() { - $data = array( - 'foo' => array('bar' => array('baz' => array('qux' => array('quux' => 'WIN!')))), - 'a' => array('b' => array('c' => array('d' => array('e' => 'abcs')))), - 'one' => array( - 'one' => 'one-one', - 'two' => 'one-two', - 'three' => 'one-three', - ), - ); - - $m = new Mustache('', $data); - $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar.baz.qux.quux}}'), 'WIN!'); - $this->assertEquals($m->render('{{%DOT-NOTATION}}{{a.b.c.d.e}}'), 'abcs'); - $this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three'); - } - - public function testDotNotationContext() { - $data = array('parent' => array('items' => array( - array('item' => array('index' => 1)), - array('item' => array('index' => 2)), - array('item' => array('index' => 3)), - array('item' => array('index' => 4)), - array('item' => array('index' => 5)), - ))); - - $m = new Mustache('', $data); - $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent}}{{#items}}{{item.index}}{{/items}}{{/parent}}')); - } - - public function testDotNotationSectionNames() { - $data = array('parent' => array('items' => array( - array('item' => array('index' => 1)), - array('item' => array('index' => 2)), - array('item' => array('index' => 3)), - array('item' => array('index' => 4)), - array('item' => array('index' => 5)), - ))); - - $m = new Mustache('', $data); - $this->assertEquals('.....', $m->render('{{%DOT-NOTATION}}{{#parent.items}}.{{/parent.items}}')); - $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{item.index}}{{/parent.items}}')); - $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{#item}}{{index}}{{/item}}{{/parent.items}}')); - } -} \ No newline at end of file diff --git a/test/MustachePragmaImplicitIteratorTest.php b/test/MustachePragmaImplicitIteratorTest.php deleted file mode 100644 index 507620d..0000000 --- a/test/MustachePragmaImplicitIteratorTest.php +++ /dev/null @@ -1,87 +0,0 @@ -getMock('Mustache', array('_renderPragma'), array('{{%IMPLICIT-ITERATOR}}')); - $m->expects($this->exactly(1)) - ->method('_renderPragma') - ->with(array( - 0 => '{{%IMPLICIT-ITERATOR}}', - 1 => 'IMPLICIT-ITERATOR', 'pragma_name' => 'IMPLICIT-ITERATOR', - 2 => null, 'options_string' => null - )); - $m->render(); - } - - public function testImplicitIterator() { - $m1 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array('a', 'b', 'c'))); - $this->assertEquals('abc', $m1->render()); - - $m2 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array(1, 2, 3))); - $this->assertEquals('123', $m2->render()); - } - - public function testDotNotationCollision() { - $m = new Mustache(null, array('items' => array('foo', 'bar', 'baz'))); - - $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{%DOT-NOTATION}}{{#items}}{{.}}{{/items}}')); - $this->assertEquals('foobarbaz', $m->render('{{%DOT-NOTATION}}{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}')); - } - - public function testCustomIterator() { - $m = new Mustache(null, array('items' => array('foo', 'bar', 'baz'))); - - $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}')); - $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{#items}}{{i}}{{/items}}')); - $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=items}}{{#items}}{{items}}{{/items}}')); - } - - public function testDotNotationContext() { - $m = new Mustache(null, array('items' => array( - array('index' => 1, 'name' => 'foo'), - array('index' => 2, 'name' => 'bar'), - array('index' => 3, 'name' => 'baz'), - ))); - - $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{name}}{{/.}}{{/items}}')); - $this->assertEquals('123', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.index}}{{/items}}')); - $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.name}}{{/items}}')); - } - - /** - * @dataProvider recursiveSectionData - */ - public function testRecursiveSections($template, $view, $result) { - $m = new Mustache(); - $this->assertEquals($result, $m->render($template, $view)); - } - - public function recursiveSectionData() { - return array( - array( - '{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{.}}{{/.}}{{/items}}', - array('items' => array(array('a', 'b', 'c'), array('d', 'e', 'f'))), - 'abcdef' - ), - array( - '{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{#.}}{{.}}{{/.}}{{/.}}{{/items}}', - array('items' => array(array(array('a', 'b'), array('c')), array(array('d'), array('e', 'f')))), - 'abcdef' - ), - array( - '{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{#items}}{{.}}{{/items}}{{/.}}{{/items}}', - array('items' => array( - array('items' => array('a', 'b', 'c')), - array('items' => array('d', 'e', 'f')), - )), - 'abcdef' - ), - ); - } -} \ No newline at end of file diff --git a/test/MustachePragmaTest.php b/test/MustachePragmaTest.php index 8be6e66..7952b03 100644 --- a/test/MustachePragmaTest.php +++ b/test/MustachePragmaTest.php @@ -22,23 +22,23 @@ class MustachePragmaTest extends PHPUnit_Framework_TestCase { public function testPragmaReplace() { $m = new Mustache(); - $this->assertEquals('', $m->render('{{%DOT-NOTATION}}'), 'Pragma tag not removed'); + $this->assertEquals('', $m->render('{{%UNESCAPED}}'), 'Pragma tag not removed'); } public function testPragmaReplaceMultiple() { $m = new Mustache(); - $this->assertEquals('', $m->render('{{% DOT-NOTATION }}'), 'Pragmas should allow whitespace'); - $this->assertEquals('', $m->render('{{% DOT-NOTATION foo=bar }}'), 'Pragmas should allow whitespace'); - $this->assertEquals('', $m->render("{{%DOT-NOTATION}}\n{{%DOT-NOTATION}}"), 'Multiple pragma tags not removed'); - $this->assertEquals(' ', $m->render('{{%DOT-NOTATION}} {{%DOT-NOTATION}}'), 'Multiple pragma tags not removed'); + $this->assertEquals('', $m->render('{{% UNESCAPED }}'), 'Pragmas should allow whitespace'); + $this->assertEquals('', $m->render('{{% UNESCAPED foo=bar }}'), 'Pragmas should allow whitespace'); + $this->assertEquals('', $m->render("{{%UNESCAPED}}\n{{%UNESCAPED}}"), 'Multiple pragma tags not removed'); + $this->assertEquals(' ', $m->render('{{%UNESCAPED}} {{%UNESCAPED}}'), 'Multiple pragma tags not removed'); } public function testPragmaReplaceNewline() { $m = new Mustache(); - $this->assertEquals('', $m->render("{{%DOT-NOTATION}}\n"), 'Trailing newline after pragma tag not removed'); - $this->assertEquals("\n", $m->render("\n{{%DOT-NOTATION}}\n"), 'Too many newlines removed with pragma tag'); - $this->assertEquals("1\n23", $m->render("1\n2{{%DOT-NOTATION}}\n3"), 'Wrong newline removed with pragma tag'); + $this->assertEquals('', $m->render("{{%UNESCAPED}}\n"), 'Trailing newline after pragma tag not removed'); + $this->assertEquals("\n", $m->render("\n{{%UNESCAPED}}\n"), 'Too many newlines removed with pragma tag'); + $this->assertEquals("1\n23", $m->render("1\n2{{%UNESCAPED}}\n3"), 'Wrong newline removed with pragma tag'); } public function testPragmaReset() { diff --git a/test/MustacheTest.php b/test/MustacheTest.php index 55d5204..6bb19d7 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -104,11 +104,11 @@ class MustacheTest extends PHPUnit_Framework_TestCase { array( 'charset' => 'cp866', 'delimiters' => array('[[[[', ']]]]'), - 'pragmas' => array(Mustache::PRAGMA_DOT_NOTATION, Mustache::PRAGMA_IMPLICIT_ITERATOR) + 'pragmas' => array(Mustache::PRAGMA_UNESCAPED) ), 'cp866', array('[[[[', ']]]]'), - array(Mustache::PRAGMA_DOT_NOTATION, Mustache::PRAGMA_IMPLICIT_ITERATOR), + array(Mustache::PRAGMA_UNESCAPED), ), ); }