From 0cadc543f857c54448d5a83a8178807dee4294b8 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 12 May 2024 21:45:32 -0400 Subject: [PATCH] Fix non-string lambda sections, add some tests. --- src/Mustache/Compiler.php | 19 +++++--- src/Mustache/Template.php | 4 +- .../Test/FiveThree/Functional/FiltersTest.php | 43 +++++++++++++++++++ .../Functional/HigherOrderSectionsTest.php | 27 ++++++++++++ 4 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index 7da49d1e..995736ca 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -333,15 +333,20 @@ private function section%s(Mustache_Context $context, $indent, $value) if (%s) { $source = %s; - $result = (string) call_user_func($value, $source, %s); - if (strpos($result, \'{{\') === false) { - $buffer .= $result; - } else { - $buffer .= $this->mustache - ->loadLambda($result%s) + $value = call_user_func($value, $source, %s); + + if (is_string($value)) { + if (strpos($value, \'{{\') === false) { + return $value; + } + + return $this->mustache + ->loadLambda($value%s) ->renderInternal($context); } - } elseif (!empty($value)) { + } + + if (!empty($value)) { $values = $this->isIterable($value) ? $value : array($value); foreach ($values as $value) { $context->push($value); diff --git a/src/Mustache/Template.php b/src/Mustache/Template.php index 67f82833..b8db2ccd 100644 --- a/src/Mustache/Template.php +++ b/src/Mustache/Template.php @@ -176,9 +176,9 @@ protected function resolveValue($value, Mustache_Context $context) return $this->mustache ->loadLambda($result) ->renderInternal($context); - } else { - return $result; } + + return $result; } return $value; diff --git a/test/Mustache/Test/FiveThree/Functional/FiltersTest.php b/test/Mustache/Test/FiveThree/Functional/FiltersTest.php index 16dec608..96e952e1 100644 --- a/test/Mustache/Test/FiveThree/Functional/FiltersTest.php +++ b/test/Mustache/Test/FiveThree/Functional/FiltersTest.php @@ -184,4 +184,47 @@ public function brokenPipeData() })), ); } + + /** + * @group lambdas + * @dataProvider lambdaFiltersData + */ + public function testLambdaFilters($tpl, $data, $expect) + { + $this->assertEquals($expect, $this->mustache->render($tpl, $data)); + } + + public function lambdaFiltersData() + { + $people = array( + (object) array('name' => 'Albert'), + (object) array('name' => 'Betty'), + (object) array('name' => 'Charles'), + ); + + $data = array( + 'people' => $people, + 'first_name' => function ($arr) { + return $arr[0]->name; + }, + 'last_name' => function ($arr) { + $last = end($arr); + + return $last->name; + }, + 'all_names' => function ($arr) { + return implode(', ', array_map(function ($person) { return $person->name; }, $arr)); + }, + 'first_person' => function ($arr) { + return $arr[0]; + }, + ); + + return array( + array('{{% FILTERS }}{{ people | first_name }}', $data, 'Albert'), + array('{{% FILTERS }}{{ people | last_name }}', $data, 'Charles'), + array('{{% FILTERS }}{{ people | all_names }}', $data, 'Albert, Betty, Charles'), + array('{{% FILTERS }}{{# people | first_person }}{{ name }}{{/ people }}', $data, 'Albert'), + ); + } } diff --git a/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php b/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php index eb051504..d7fa2cb5 100644 --- a/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php +++ b/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php @@ -60,6 +60,33 @@ public function testViewArrayAnonymousSectionCallback() $this->assertEquals(sprintf('[[%s]]', $data['name']), $tpl->render($data)); } + + /** + * @dataProvider nonTemplateLambdasData + */ + public function testNonTemplateLambdas($tpl, $data, $expect) + { + $this->assertEquals($expect, $this->mustache->render($tpl, $data)); + } + + public function nonTemplateLambdasData() + { + $data = array( + 'lang' => 'en-US', + 'people' => function () { + return array( + (object) array('name' => 'Albert', 'lang' => 'en-GB'), + (object) array('name' => 'Betty'), + (object) array('name' => 'Charles'), + ); + }, + ); + + return array( + array("{{# people }} - {{ name }}\n{{/people}}", $data, " - Albert\n - Betty\n - Charles\n"), + array("{{# people }} - {{ name }}: {{ lang }}\n{{/people}}", $data, " - Albert: en-GB\n - Betty: en-US\n - Charles: en-US\n"), + ); + } } class Mustache_Test_FiveThree_Functional_Foo