diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index c13e60fc..db17d90c 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -17,6 +17,7 @@ class Mustache_Compiler { + private $pragmas; private $sections; private $source; private $indentNextLine; @@ -24,7 +25,6 @@ class Mustache_Compiler private $entityFlags; private $charset; private $strictCallables; - private $pragmas; /** * Compile a Mustache token parse tree into PHP source code. @@ -94,7 +94,6 @@ private function walk(array $tree, $level = 0) break; case Mustache_Tokenizer::T_PARTIAL: - case Mustache_Tokenizer::T_PARTIAL_2: $code .= $this->partial( $node[Mustache_Tokenizer::NAME], isset($node[Mustache_Tokenizer::INDENT]) ? $node[Mustache_Tokenizer::INDENT] : '', @@ -102,6 +101,39 @@ private function walk(array $tree, $level = 0) ); break; + case Mustache_Tokenizer::T_PARENT: + $code .= $this->parent( + $node[Mustache_Tokenizer::NAME], + isset($node[Mustache_Tokenizer::INDENT]) ? $node[Mustache_Tokenizer::INDENT] : '', + $level, + $node[Mustache_Tokenizer::NODES] + ); + break; + + case Mustache_Tokenizer::T_BLOCK_ARG: + $code .= $this->blockArg( + $node[Mustache_Tokenizer::NODES], + $node[Mustache_Tokenizer::NAME], + $node[Mustache_Tokenizer::INDEX], + $node[Mustache_Tokenizer::END], + $node[Mustache_Tokenizer::OTAG], + $node[Mustache_Tokenizer::CTAG], + $level + ); + break; + + case Mustache_Tokenizer::T_BLOCK_VAR: + $code .= $this->blockVar( + $node[Mustache_Tokenizer::NODES], + $node[Mustache_Tokenizer::NAME], + $node[Mustache_Tokenizer::INDEX], + $node[Mustache_Tokenizer::END], + $node[Mustache_Tokenizer::OTAG], + $node[Mustache_Tokenizer::CTAG], + $level + ); + break; + case Mustache_Tokenizer::T_UNESCAPED: case Mustache_Tokenizer::T_UNESCAPED_2: $code .= $this->variable($node[Mustache_Tokenizer::NAME], false, $level); @@ -122,7 +154,6 @@ private function walk(array $tree, $level = 0) throw new Mustache_Exception_SyntaxException(sprintf('Unknown token type: %s', $node[Mustache_Tokenizer::TYPE]), $node); } } - return $code; } @@ -136,6 +167,7 @@ public function renderInternal(Mustache_Context $context, $indent = \'\') { $this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context); $buffer = \'\'; + $newContext = array(); %s return $buffer; @@ -150,6 +182,7 @@ class %s extends Mustache_Template public function renderInternal(Mustache_Context $context, $indent = \'\') { $buffer = \'\'; + $newContext = array(); %s return $buffer; @@ -171,11 +204,49 @@ private function writeCode($tree, $name) $code = $this->walk($tree); $sections = implode("\n", $this->sections); $klass = empty($this->sections) ? self::KLASS_NO_LAMBDAS : self::KLASS; + $callable = $this->strictCallables ? $this->prepare(self::STRICT_CALLABLE) : ''; return sprintf($this->prepare($klass, 0, false, true), $name, $callable, $code, $sections); } + const BLOCK_VAR = ' + $value = $this->resolveValue($context->findInBlock(%s), $context, $indent); + if($value && !is_array($value) && !is_object($value)) { + $buffer .= $value; + } else { + %s + } + '; + + private function blockVar($nodes, $id, $start, $end, $otag, $ctag, $level) + { + $id_str = var_export($id, true); + + return sprintf($this->prepare(self::BLOCK_VAR, $level), $id_str, $this->walk($nodes, 2)); + } + + const BLOCK_ARG = ' + // %s block_arg + $value = $this->section%s($context, $indent, true); + $newContext[%s] = %s$value; + '; + + private function blockArg($nodes, $id, $start, $end, $otag, $ctag, $level) + { + $key = $this->section($nodes, $id, $start, $end, $otag, $ctag, $level, true); + $filters = ''; + + if (isset($this->pragmas[Mustache_Engine::PRAGMA_FILTERS])) { + list($id, $filters) = $this->getFilters($id, $level); + } + + $method = $this->getFindMethod($id); + $id = var_export($id, true); + + return sprintf($this->prepare(self::BLOCK_ARG, $level), $id, $key, $id, $this->flushIndent()); + } + const SECTION_CALL = ' // %s section $value = $context->%s(%s);%s @@ -199,7 +270,8 @@ private function section%s(Mustache_Context $context, $indent, $value) } elseif (!empty($value)) { $values = $this->isIterable($value) ? $value : array($value); foreach ($values as $value) { - $context->push($value);%s + $context->push($value); + %s $context->pop(); } } @@ -220,7 +292,7 @@ private function section%s(Mustache_Context $context, $indent, $value) * * @return string Generated section PHP source code */ - private function section($nodes, $id, $start, $end, $otag, $ctag, $level) + private function section($nodes, $id, $start, $end, $otag, $ctag, $level, $arg=false) { $filters = ''; @@ -228,8 +300,6 @@ private function section($nodes, $id, $start, $end, $otag, $ctag, $level) list($id, $filters) = $this->getFilters($id, $level); } - $method = $this->getFindMethod($id); - $id = var_export($id, true); $source = var_export(substr($this->source, $start, $end - $start), true); $callable = $this->getCallable(); @@ -245,7 +315,13 @@ private function section($nodes, $id, $start, $end, $otag, $ctag, $level) $this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $delims, $this->walk($nodes, 2)); } - return sprintf($this->prepare(self::SECTION_CALL, $level), $id, $method, $id, $filters, $key); + if ($arg === true) { + return $key; + } else { + $method = $this->getFindMethod($id); + $id = var_export($id, true); + return sprintf($this->prepare(self::SECTION_CALL, $level), $id, $method, $id, $filters, $key); + } } const INVERTED_SECTION = ' @@ -302,6 +378,35 @@ private function partial($id, $indent, $level) ); } + const PARENT = ' + + if ($parent = $this->mustache->LoadPartial(%s)) { + $context->pushBlockContext($newContext); + $buffer .= $parent->renderInternal($context, $indent); + $context->popBlockContext(); + } + '; + + private function parent($id, $indent, $level, $children) + { + $block = ''; + + $real_children = array_filter($children, array(__CLASS__, 'return_only_block_args')); + + $block = $this->walk($real_children, $level); + + return $block. sprintf( + $this->prepare(self::PARENT, $level), + var_export($id, true), + var_export($indent, true) + ); + } + + private static function return_only_block_args($child) + { + return $child[Mustache_Tokenizer::TYPE] == Mustache_Tokenizer::T_BLOCK_ARG; + } + const VARIABLE = ' $value = $this->resolveValue($context->%s(%s), $context, $indent);%s $buffer .= %s%s; diff --git a/src/Mustache/Context.php b/src/Mustache/Context.php index c6900d7c..96089ced 100644 --- a/src/Mustache/Context.php +++ b/src/Mustache/Context.php @@ -15,6 +15,7 @@ class Mustache_Context { private $stack = array(); + private $block_stack = array(); /** * Mustache rendering Context constructor. @@ -38,6 +39,11 @@ public function push($value) array_push($this->stack, $value); } + public function pushBlockContext($value) + { + array_push($this->block_stack, $value); + } + /** * Pop the last Context frame from the stack. * @@ -48,6 +54,11 @@ public function pop() return array_pop($this->stack); } + public function popBlockContext() + { + return array_pop($this->block_stack); + } + /** * Get the last Context frame. * @@ -120,6 +131,17 @@ public function findDot($id) return $value; } + public function findInBlock($id) + { + foreach($this->block_stack as $context) { + if (array_key_exists($id, $context)) { + return $context[$id]; + } + } + + return ''; + } + /** * Helper function to find a variable in the Context stack. * diff --git a/src/Mustache/Parser.php b/src/Mustache/Parser.php index f44153c0..d331db29 100644 --- a/src/Mustache/Parser.php +++ b/src/Mustache/Parser.php @@ -60,11 +60,13 @@ private function buildTree(array &$tokens, array $parent = null) switch ($token[Mustache_Tokenizer::TYPE]) { case Mustache_Tokenizer::T_DELIM_CHANGE: + $this->checkIfTokenIsAllowedInParent($parent, $token); $this->clearStandaloneLines($nodes, $tokens); break; case Mustache_Tokenizer::T_SECTION: case Mustache_Tokenizer::T_INVERTED: + $this->checkIfTokenIsAllowedInParent($parent, $token); $this->clearStandaloneLines($nodes, $tokens); $nodes[] = $this->buildTree($tokens, $token); break; @@ -95,17 +97,29 @@ private function buildTree(array &$tokens, array $parent = null) $parent[Mustache_Tokenizer::NODES] = $nodes; return $parent; - break; case Mustache_Tokenizer::T_PARTIAL: - case Mustache_Tokenizer::T_PARTIAL_2: - // store the whitespace prefix for laters! + $this->checkIfTokenIsAllowedInParent($parent, $token); + //store the whitespace prefix for laters! if ($indent = $this->clearStandaloneLines($nodes, $tokens)) { $token[Mustache_Tokenizer::INDENT] = $indent[Mustache_Tokenizer::VALUE]; } $nodes[] = $token; break; + case Mustache_Tokenizer::T_PARENT: + $this->checkIfTokenIsAllowedInParent($parent, $token); + $nodes[] = $this->buildTree($tokens, $token); + break; + + case Mustache_Tokenizer::T_BLOCK_VAR: + if ($parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) { + $token[Mustache_Tokenizer::TYPE] = Mustache_Tokenizer::T_BLOCK_ARG; + } + $this->clearStandaloneLines($nodes, $tokens); + $nodes[] = $this->buildTree($tokens, $token); + break; + case Mustache_Tokenizer::T_PRAGMA: case Mustache_Tokenizer::T_COMMENT: $this->clearStandaloneLines($nodes, $tokens); @@ -205,4 +219,11 @@ private function tokenIsWhitespace(array $token) return false; } + + private function checkIfTokenIsAllowedInParent($parent, $token) + { + if ($parent[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_PARENT) { + throw new Mustache_Exception_SyntaxException('Illegal content in < parent tag', $token); + } + } } diff --git a/src/Mustache/Template.php b/src/Mustache/Template.php index 8c769161..b64cd26f 100644 --- a/src/Mustache/Template.php +++ b/src/Mustache/Template.php @@ -64,7 +64,9 @@ public function __invoke($context = array()) */ public function render($context = array()) { - return $this->renderInternal($this->prepareContextStack($context)); + return $this->renderInternal( + $this->prepareContextStack($context) + ); } /** diff --git a/src/Mustache/Tokenizer.php b/src/Mustache/Tokenizer.php index c701b258..c8514344 100644 --- a/src/Mustache/Tokenizer.php +++ b/src/Mustache/Tokenizer.php @@ -28,13 +28,15 @@ class Mustache_Tokenizer const T_END_SECTION = '/'; const T_COMMENT = '!'; const T_PARTIAL = '>'; - const T_PARTIAL_2 = '<'; + const T_PARENT = '<'; const T_DELIM_CHANGE = '='; const T_ESCAPED = '_v'; const T_UNESCAPED = '{'; const T_UNESCAPED_2 = '&'; const T_TEXT = '_t'; const T_PRAGMA = '%'; + const T_BLOCK_VAR = '$'; + const T_BLOCK_ARG = '$arg'; // Valid token types private static $tagTypes = array( @@ -43,12 +45,13 @@ class Mustache_Tokenizer self::T_END_SECTION => true, self::T_COMMENT => true, self::T_PARTIAL => true, - self::T_PARTIAL_2 => true, + self::T_PARENT => true, self::T_DELIM_CHANGE => true, self::T_ESCAPED => true, self::T_UNESCAPED => true, self::T_UNESCAPED_2 => true, self::T_PRAGMA => true, + self::T_BLOCK_VAR => true, ); // Interpolated tags diff --git a/test/Mustache/Test/Functional/InheritanceTest.php b/test/Mustache/Test/Functional/InheritanceTest.php new file mode 100644 index 00000000..1f5dd973 --- /dev/null +++ b/test/Mustache/Test/Functional/InheritanceTest.php @@ -0,0 +1,437 @@ +mustache = new Mustache_Engine; + } + + public function getIllegalInheritanceExamples() + { + return array( + array( + array( + 'foo' => '{{$baz}}default content{{/baz}}', + ), + array( + 'bar' => 'set by user' + ), + '{{< foo }}{{# bar }}{{$ baz }}{{/ baz }}{{/ bar }}{{/ foo }}', + ), + array( + array( + 'foo' => '{{$baz}}default content{{/baz}}' + ), + array( + ), + '{{ '{{$baz}}default content{{/baz}}', + 'qux' => 'I am a partial' + ), + array( + ), + '{{qux}}{{$baz}}set by template{{/baz}}{{/foo}}' + ), + array( + array( + 'foo' => '{{$baz}}default content{{/baz}}' + ), + array(), + '{{=}}<%={{ }}=%>{{/foo}}' + ) + ); + } + + public function getLegalInheritanceExamples() + { + return array( + array( + array( + 'foo' => '{{$baz}}default content{{/baz}}', + ), + array( + 'bar' => 'set by user' + ), + '{{ '{{$baz}}default content{{/baz}}' + ), + array( + ), + '{{ '{{$baz}}defualt content{{/baz}}' + ), + array(), + '{{mustache->loadTemplate('{{$title}}Default title{{/title}}'); + + $data = array(); + + $this->assertEquals('Default title', $tpl->render($data)); + } + + public function testDefaultContentRendersVariables() + { + $tpl = $this->mustache->loadTemplate('{{$foo}}default {{bar}} content{{/foo}}'); + + $data = array( + 'bar' => 'baz' + ); + + $this->assertEquals('default baz content', $tpl->render($data)); + } + + public function testDefaultContentRendersTripleMustacheVariables() + { + $tpl = $this->mustache->loadTemplate('{{$foo}}default {{{bar}}} content{{/foo}}'); + + $data = array( + 'bar' => '' + ); + + $this->assertEquals('default content', $tpl->render($data)); + } + + public function testDefaultContentRendersSections() + { + $tpl = $this->mustache->loadTemplate( + '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}' + ); + + $data = array( + 'bar' => array('baz' => 'qux') + ); + + $this->assertEquals('default qux content', $tpl->render($data)); + } + + public function testDefaultContentRendersNegativeSections() + { + $tpl = $this->mustache->loadTemplate( + '{{$foo}}default {{^bar}}{{baz}}{{/bar}} content{{/foo}}' + ); + + $data = array( + 'foo' => array('bar' => 'qux'), + 'baz' => 'three' + ); + + $this->assertEquals('default three content', $tpl->render($data)); + + } + + public function testMustacheInjectionInDefaultContent() + { + $tpl = $this->mustache->loadTemplate( + '{{$foo}}default {{#bar}}{{baz}}{{/bar}} content{{/foo}}' + ); + + $data = array( + 'bar' => array('baz' => '{{qux}}') + ); + + $this->assertEquals('default {{qux}} content', $tpl->render($data)); + } + + public function testDefaultContentRenderedInsideIncludedTemplates() + { + $partials = array( + 'include' => '{{$foo}}default content{{/foo}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('default content', $tpl->render($data)); + } + + public function testOverriddenContent() + { + $partials = array( + 'super' => '...{{$title}}Default title{{/title}}...' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('...sub template title...', $tpl->render($data)); + } + + public function testOverriddenPartial() + { + $partials = array( + 'partial' => '|{{$stuff}}...{{/stuff}}{{$default}} default{{/default}}|' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + 'test {{assertEquals('test |override1 default| |override2 default|', $tpl->render($data)); + } + + public function testDataDoesNotOverrideBlock() + { + $partials = array( + 'include' => '{{$var}}var in include{{/var}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{ 'var in data' + ); + + $this->assertEquals('var in template', $tpl->render($data)); + } + + public function testDataDoesNotOverrideDefaultBlockValue() + { + $partials = array( + 'include' => '{{$var}}var in include{{/var}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{ 'var in data' + ); + + $this->assertEquals('var in include', $tpl->render($data)); + } + + public function testOverridePartialWithNewlines() + { + $partials = array( + 'partial' => '{{$ballmer}}peaking{{/ballmer}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + "{{assertEquals("peaked\n\n:(\n", $tpl->render($data)); + } + + public function testInheritIndentationWhenOverridingAPartial() + { + $partials = array( + 'partial' => + 'stop: + {{$nineties}}collaborate and listen{{/nineties}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals( + 'stop: + hammer time', + $tpl->render($data) + ); + } + + public function testOverrideOneSubstitutionButNotTheOther() + { + $partials = array( + 'partial' => '{{$stuff}}default one{{/stuff}}, {{$stuff2}}default two{{/stuff2}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('default one, override two', $tpl->render($data)); + } + + public function testSuperTemplatesWithNoParameters() + { + $partials = array( + 'include' => '{{$foo}}default content{{/foo}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{>include}}|{{assertEquals('default content|default content', $tpl->render($data)); + } + + public function testRecursionInInheritedTemplates() + { + $partials = array( + 'include' => '{{$foo}}default content{{/foo}} {{$bar}}{{ '{{$foo}}include2 default content{{/foo}} {{mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('override override override don\'t recurse', $tpl->render($data)); + } + + public function testTopLevelSubstitutionsTakePrecedenceInMultilevelInheritance() + { + $partials = array( + 'parent' => '{{ '{{ '{{$a}}g{{/a}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('c', $tpl->render($data)); + } + + public function testMultiLevelInheritanceNoSubChild() + { + $partials = array( + 'parent' => '{{ '{{ '{{$a}}g{{/a}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('p', $tpl->render($data)); + } + + public function testIgnoreTextInsideSuperTemplatesButParseArgs() + { + $partials = array( + 'include' => '{{$foo}}default content{{/foo}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('hmm', $tpl->render($data)); + } + + public function testIgnoreTextInsideSuperTemplates() + { + $partials = array( + 'include' => '{{$foo}}default content{{/foo}}' + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{assertEquals('default content', $tpl->render($data)); + } + + + + /** + * @dataProvider getIllegalInheritanceExamples + * @expectedException Mustache_Exception_SyntaxException + * @expectedExceptionMessage Illegal content in < parent tag + */ + public function testIllegalInheritanceExamples($partials, $data, $template) + { + $this->mustache->setPartials($partials); + $tpl = $this->mustache->loadTemplate($template); + $tpl->render($data); + } + + /** + * @dataProvider getLegalInheritanceExamples + */ + public function testLegalInheritanceExamples($partials, $data, $template, $expect) + { + $this->mustache->setPartials($partials); + $tpl = $this->mustache->loadTemplate($template); + $this->assertSame($expect, $tpl->render($data)); + } +} diff --git a/test/Mustache/Test/ParserTest.php b/test/Mustache/Test/ParserTest.php index 57e418a8..b54e427f 100644 --- a/test/Mustache/Test/ParserTest.php +++ b/test/Mustache/Test/ParserTest.php @@ -88,6 +88,7 @@ public function getTokenSets() Mustache_Tokenizer::VALUE => 'bar' ), ), + array( array( Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT, @@ -116,6 +117,118 @@ public function getTokenSets() ), ), + array( + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_PARENT, + Mustache_Tokenizer::NAME => 'foo', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 8 + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_BLOCK_VAR, + Mustache_Tokenizer::NAME => 'bar', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 16 + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT, + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::VALUE => 'baz' + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION, + Mustache_Tokenizer::NAME => 'bar', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 19 + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION, + Mustache_Tokenizer::NAME => 'foo', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 27 + ) + ), + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_PARENT, + Mustache_Tokenizer::NAME => 'foo', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 8, + Mustache_Tokenizer::END => 27, + Mustache_Tokenizer::NODES => array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_BLOCK_ARG, + Mustache_Tokenizer::NAME => 'bar', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 16, + Mustache_Tokenizer::END => 19, + Mustache_Tokenizer::NODES => array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT, + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::VALUE => 'baz' + ) + ) + ) + ) + ) + ) + ), + + array( + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_BLOCK_VAR, + Mustache_Tokenizer::NAME => 'foo', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT, + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::VALUE => 'bar' + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION, + Mustache_Tokenizer::NAME => 'foo', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 11, + ), + ), + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_BLOCK_VAR, + Mustache_Tokenizer::NAME => 'foo', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::END => 11, + Mustache_Tokenizer::NODES => array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT, + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::VALUE => 'bar' + ) + ) + ) + ) + ) ); } diff --git a/test/Mustache/Test/TokenizerTest.php b/test/Mustache/Test/TokenizerTest.php index 63a94d48..629e5437 100644 --- a/test/Mustache/Test/TokenizerTest.php +++ b/test/Mustache/Test/TokenizerTest.php @@ -188,6 +188,35 @@ public function getTokens() ), ) ), + + // Ensure that $arg token is not picked up during tokenization + array( + '{{$arg}}default{{/arg}}', + null, + array( + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_BLOCK_VAR, + Mustache_Tokenizer::NAME => 'arg', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 8 + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_TEXT, + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::VALUE => "default", + ), + array( + Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_END_SECTION, + Mustache_Tokenizer::NAME => 'arg', + Mustache_Tokenizer::OTAG => '{{', + Mustache_Tokenizer::CTAG => '}}', + Mustache_Tokenizer::LINE => 0, + Mustache_Tokenizer::INDEX => 15, + ) + ) + ), ); } }