From 865de4114adf42368782810c267c33abb1d896d6 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 14 Feb 2016 08:43:38 -0800 Subject: [PATCH] Make LambdaHelper invokable. Fixes #285 --- src/Mustache/LambdaHelper.php | 12 ++++++++++++ .../FiveThree/Functional/LambdaHelperTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Mustache/LambdaHelper.php b/src/Mustache/LambdaHelper.php index 07532ef..cb8e966 100644 --- a/src/Mustache/LambdaHelper.php +++ b/src/Mustache/LambdaHelper.php @@ -50,6 +50,18 @@ class Mustache_LambdaHelper ->renderInternal($this->context); } + /** + * Render a string as a Mustache template with the current rendering context. + * + * @param string $string + * + * @return string Rendered template + */ + public function __invoke($string) + { + return $this->render($string); + } + /** * Get a Lambda Helper with custom delimiters. * diff --git a/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php index 1dc1997..48f2465 100644 --- a/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php +++ b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php @@ -49,4 +49,19 @@ class Mustache_Test_FiveThree_Functional_LambdaHelperTest extends PHPUnit_Framew $this->assertEquals('hello world!', $tpl->render($data)); } + + public function testLambdaHelperIsInvokable() + { + $one = $this->mustache->loadTemplate('{{name}}'); + $two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}'); + + $foo = new StdClass(); + $foo->name = 'Mario'; + $foo->lambda = function ($text, $render) { + return strtoupper($render($text)); + }; + + $this->assertEquals('Mario', $one->render($foo)); + $this->assertEquals('MARIO', $two->render($foo)); + } }