diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index e7f34e7..aff8e88 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -118,8 +118,11 @@ class Mustache_Compiler class %s extends Mustache_Template { + private $lambdaHelper; + public function renderInternal(Mustache_Context $context, $indent = \'\', $escape = false) { + $this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context); $buffer = \'\'; %s @@ -159,7 +162,7 @@ class Mustache_Compiler if (!is_string($value) && is_callable($value)) { $source = %s; $buffer .= $this->mustache - ->loadLambda((string) call_user_func($value, $source)%s) + ->loadLambda((string) call_user_func($value, $source, $this->lambdaHelper)%s) ->renderInternal($context, $indent); } elseif (!empty($value)) { $values = $this->isIterable($value) ? $value : array($value); diff --git a/src/Mustache/LambdaHelper.php b/src/Mustache/LambdaHelper.php new file mode 100644 index 0000000..6f226c5 --- /dev/null +++ b/src/Mustache/LambdaHelper.php @@ -0,0 +1,48 @@ +mustache = $mustache; + $this->context = $context; + } + + /** + * Render a string as a Mustache template with the current rendering context. + * + * @param string $string + * + * @return Rendered template. + */ + public function render($string) + { + return $this->mustache + ->loadLambda((string) $string) + ->renderInternal($this->context); + } +} diff --git a/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php new file mode 100644 index 0000000..e095d35 --- /dev/null +++ b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php @@ -0,0 +1,39 @@ +mustache = new Mustache_Engine; + } + + public function testSectionLambdaHelper() + { + $one = $this->mustache->loadTemplate('{{name}}'); + $two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}'); + + $foo = new StdClass; + $foo->name = 'Mario'; + $foo->lambda = function($text, $mustache) { + return strtoupper($mustache->render($text)); + }; + + $this->assertEquals('Mario', $one->render($foo)); + $this->assertEquals('MARIO', $two->render($foo)); + } +}