From 52c7ccf1b9fc8c13c331ae63484db71f25db50d5 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Mon, 23 Jul 2012 11:56:07 -0700 Subject: [PATCH 1/2] Pass a helper to section lambdas --- src/Mustache/Compiler.php | 5 +- src/Mustache/LambdaHelper.php | 48 +++++++++++++++++++ .../FiveThree/Functional/LambdaHelperTest.php | 37 ++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/Mustache/LambdaHelper.php create mode 100644 test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php 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..a414d65 --- /dev/null +++ b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php @@ -0,0 +1,37 @@ +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)); + } +} From cc7d164a425973ae67a9a918943bf098a223b30a Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Thu, 2 Aug 2012 10:32:35 -0700 Subject: [PATCH 2/2] Coding standards fix. --- .../Test/FiveThree/Functional/LambdaHelperTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php index a414d65..e095d35 100644 --- a/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php +++ b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php @@ -13,15 +13,17 @@ * @group lambdas * @group functional */ -class Mustache_Test_FiveThree_Functional_LambdaHelperTest extends PHPUnit_Framework_TestCase { - +class Mustache_Test_FiveThree_Functional_LambdaHelperTest extends PHPUnit_Framework_TestCase +{ private $mustache; - public function setUp() { + public function setUp() + { $this->mustache = new Mustache_Engine; } - public function testSectionLambdaHelper() { + public function testSectionLambdaHelper() + { $one = $this->mustache->loadTemplate('{{name}}'); $two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}');