From 11ad87ae2d4a5db52fa5831ca1921784145600ed Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Thu, 16 Jul 2015 16:24:02 +0800 Subject: [PATCH] Lazily evaluate blocks pragmas. The specific example in the docs page about what does not work, is exactly what we need to make this useful. Fixes #264 --- src/Mustache/Compiler.php | 21 +++++++----- .../Test/Functional/InheritanceTest.php | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index e25bd8c..bf5f808 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -233,9 +233,10 @@ class Mustache_Compiler } const BLOCK_VAR = ' - $value = $this->resolveValue($context->findInBlock(%s), $context, $indent); - if ($value && !is_array($value) && !is_object($value)) { - $buffer .= $value; + $blockFunction = $context->findInBlock(%s); + if (is_callable($blockFunction)) { + $boundFunction = $blockFunction->bindTo($this, $this); + $boundFunction($context, $buffer); } else { %s } @@ -258,13 +259,15 @@ class Mustache_Compiler { $id = var_export($id, true); - return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $this->walk($nodes, 2)); + return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $this->walk($nodes)); } const BLOCK_ARG = ' // %s block_arg - $value = $this->section%s($context, \'\', true); - $newContext[%s] = %s$value; + $blockFunction = function(& $context, & $buffer, $indent=\'\') { + %s + }; + $newContext[%s] = $blockFunction; '; /** @@ -282,10 +285,10 @@ class Mustache_Compiler */ private function blockArg($nodes, $id, $start, $end, $otag, $ctag, $level) { - $key = $this->section($nodes, $id, array(), $start, $end, $otag, $ctag, $level, true); - $id = var_export($id, true); + $id = var_export($id, true); + $code = $this->walk($nodes, 1); - return sprintf($this->prepare(self::BLOCK_ARG, $level), $id, $key, $id, $this->flushIndent()); + return sprintf($this->prepare(self::BLOCK_ARG, 1), $id, $code, $id); } const SECTION_CALL = ' diff --git a/test/Mustache/Test/Functional/InheritanceTest.php b/test/Mustache/Test/Functional/InheritanceTest.php index 122ed31..b2e178d 100644 --- a/test/Mustache/Test/Functional/InheritanceTest.php +++ b/test/Mustache/Test/Functional/InheritanceTest.php @@ -433,6 +433,40 @@ class Mustache_Test_Functional_InheritanceTest extends PHPUnit_Framework_TestCas $this->assertEquals('default content', $tpl->render($data)); } + public function testInheritanceWithLazyEvaluation() + { + $partials = array( + 'parent' => '{{#items}}{{$value}}ignored{{/value}}{{/items}}', + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{{{/value}}{{/parent}}' + ); + + $data = array('items' => array(1, 2, 3)); + + $this->assertEquals('<1><2><3>', $tpl->render($data)); + } + + public function testInheritanceWithLazyEvaluationWhitespaceIgnored() + { + $partials = array( + 'parent' => '{{#items}}{{$value}}\n\nignored\n\n{{/value}}{{/items}}', + ); + + $this->mustache->setPartials($partials); + + $tpl = $this->mustache->loadTemplate( + '{{{{/value}}\n\n{{/parent}}' + ); + + $data = array('items' => array(1, 2, 3)); + + $this->assertEquals('<1><2><3>', $tpl->render($data)); + } + /** * @dataProvider getIllegalInheritanceExamples * @expectedException Mustache_Exception_SyntaxException