Skip to content

Commit

Permalink
Fix non-string lambda sections, add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed May 13, 2024
1 parent 8cbda9e commit 0cadc54
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/Mustache/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Mustache/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions test/Mustache/Test/FiveThree/Functional/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0cadc54

Please sign in to comment.