Skip to content

Commit 0bb2f76

Browse files
committed
Merge branch 'release/2.10.0'
2 parents c745b01 + a9dd4c6 commit 0bb2f76

File tree

8 files changed

+110
-16
lines changed

8 files changed

+110
-16
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"php": ">=5.2.4"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "~3.7|~4.0",
19+
"phpunit/phpunit": "~3.7|~4.0|~5.0",
2020
"fabpot/php-cs-fixer": "~1.6"
2121
},
2222
"autoload": {

src/Mustache/Compiler.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private function section%s(Mustache_Context $context, $indent, $value)
328328
$buffer = \'\';
329329
if (%s) {
330330
$source = %s;
331-
$result = call_user_func($value, $source, $this->lambdaHelper);
331+
$result = call_user_func($value, $source, %s);
332332
if (strpos($result, \'{{\') === false) {
333333
$buffer .= $result;
334334
} else {
@@ -370,15 +370,18 @@ private function section($nodes, $id, $filters, $start, $end, $otag, $ctag, $lev
370370
$callable = $this->getCallable();
371371

372372
if ($otag !== '{{' || $ctag !== '}}') {
373-
$delims = ', ' . var_export(sprintf('{{= %s %s =}}', $otag, $ctag), true);
373+
$delimTag = var_export(sprintf('{{= %s %s =}}', $otag, $ctag), true);
374+
$helper = sprintf('$this->lambdaHelper->withDelimiters(%s)', $delimTag);
375+
$delims = ', ' . $delimTag;
374376
} else {
377+
$helper = '$this->lambdaHelper';
375378
$delims = '';
376379
}
377380

378381
$key = ucfirst(md5($delims . "\n" . $source));
379382

380383
if (!isset($this->sections[$key])) {
381-
$this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $delims, $this->walk($nodes, 2));
384+
$this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $helper, $delims, $this->walk($nodes, 2));
382385
}
383386

384387
if ($arg === true) {
@@ -495,7 +498,7 @@ private static function onlyBlockArgs(array $node)
495498
}
496499

497500
const VARIABLE = '
498-
$value = $this->resolveValue($context->%s(%s), $context, $indent);%s
501+
$value = $this->resolveValue($context->%s(%s), $context);%s
499502
$buffer .= %s%s;
500503
';
501504

src/Mustache/Engine.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
class Mustache_Engine
2525
{
26-
const VERSION = '2.9.0';
26+
const VERSION = '2.10.0';
2727
const SPEC_VERSION = '1.1.2';
2828

2929
const PRAGMA_FILTERS = 'FILTERS';

src/Mustache/LambdaHelper.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ class Mustache_LambdaHelper
2020
{
2121
private $mustache;
2222
private $context;
23+
private $delims;
2324

2425
/**
2526
* Mustache Lambda Helper constructor.
2627
*
2728
* @param Mustache_Engine $mustache Mustache engine instance.
2829
* @param Mustache_Context $context Rendering context.
30+
* @param string $delims Optional custom delimiters, in the format `{{= <% %> =}}`. (default: null)
2931
*/
30-
public function __construct(Mustache_Engine $mustache, Mustache_Context $context)
32+
public function __construct(Mustache_Engine $mustache, Mustache_Context $context, $delims = null)
3133
{
3234
$this->mustache = $mustache;
3335
$this->context = $context;
36+
$this->delims = $delims;
3437
}
3538

3639
/**
@@ -43,7 +46,31 @@ public function __construct(Mustache_Engine $mustache, Mustache_Context $context
4346
public function render($string)
4447
{
4548
return $this->mustache
46-
->loadLambda((string) $string)
49+
->loadLambda((string) $string, $this->delims)
4750
->renderInternal($this->context);
4851
}
52+
53+
/**
54+
* Render a string as a Mustache template with the current rendering context.
55+
*
56+
* @param string $string
57+
*
58+
* @return string Rendered template
59+
*/
60+
public function __invoke($string)
61+
{
62+
return $this->render($string);
63+
}
64+
65+
/**
66+
* Get a Lambda Helper with custom delimiters.
67+
*
68+
* @param string $delims Custom delimiters, in the format `{{= <% %> =}}`.
69+
*
70+
* @return Mustache_LambdaHelper
71+
*/
72+
public function withDelimiters($delims)
73+
{
74+
return new self($this->mustache, $this->context, $delims);
75+
}
4976
}

src/Mustache/Template.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,15 @@ protected function prepareContextStack($context = null)
164164
*
165165
* @param mixed $value
166166
* @param Mustache_Context $context
167-
* @param string $indent
168167
*
169168
* @return string
170169
*/
171-
protected function resolveValue($value, Mustache_Context $context, $indent = '')
170+
protected function resolveValue($value, Mustache_Context $context)
172171
{
173172
if (($this->strictCallables ? is_object($value) : !is_string($value)) && is_callable($value)) {
174173
return $this->mustache
175174
->loadLambda((string) call_user_func($value))
176-
->renderInternal($context, $indent);
175+
->renderInternal($context);
177176
}
178177

179178
return $value;

test/Mustache/Test/CompilerTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function getCompileValues()
5555
'ISO-8859-1',
5656
array(
5757
"\nclass Monkey extends Mustache_Template",
58-
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
58+
'$value = $this->resolveValue($context->find(\'name\'), $context);',
5959
'$buffer .= $indent . call_user_func($this->mustache->getEscape(), $value);',
6060
'return $buffer;',
6161
),
@@ -75,7 +75,7 @@ public function getCompileValues()
7575
'ISO-8859-1',
7676
array(
7777
"\nclass Monkey extends Mustache_Template",
78-
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
78+
'$value = $this->resolveValue($context->find(\'name\'), $context);',
7979
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_COMPAT . ', \'ISO-8859-1\');',
8080
'return $buffer;',
8181
),
@@ -95,7 +95,7 @@ public function getCompileValues()
9595
'ISO-8859-1',
9696
array(
9797
"\nclass Monkey extends Mustache_Template",
98-
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
98+
'$value = $this->resolveValue($context->find(\'name\'), $context);',
9999
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_QUOTES . ', \'ISO-8859-1\');',
100100
'return $buffer;',
101101
),
@@ -122,9 +122,9 @@ public function getCompileValues()
122122
array(
123123
"\nclass Monkey extends Mustache_Template",
124124
"\$buffer .= \$indent . 'foo\n';",
125-
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);',
125+
'$value = $this->resolveValue($context->find(\'name\'), $context);',
126126
'$buffer .= htmlspecialchars($value, ' . ENT_COMPAT . ', \'UTF-8\');',
127-
'$value = $this->resolveValue($context->last(), $context, $indent);',
127+
'$value = $this->resolveValue($context->last(), $context);',
128128
'$buffer .= \'\\\'bar\\\'\';',
129129
'return $buffer;',
130130
),

test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,32 @@ public function testSectionLambdaHelper()
3636
$this->assertEquals('Mario', $one->render($foo));
3737
$this->assertEquals('MARIO', $two->render($foo));
3838
}
39+
40+
public function testSectionLambdaHelperRespectsDelimiterChanges()
41+
{
42+
$tpl = $this->mustache->loadTemplate("{{=<% %>=}}\n<%# bang %><% value %><%/ bang %>");
43+
44+
$data = new StdClass();
45+
$data->value = 'hello world';
46+
$data->bang = function ($text, $mustache) {
47+
return $mustache->render($text) . '!';
48+
};
49+
50+
$this->assertEquals('hello world!', $tpl->render($data));
51+
}
52+
53+
public function testLambdaHelperIsInvokable()
54+
{
55+
$one = $this->mustache->loadTemplate('{{name}}');
56+
$two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}');
57+
58+
$foo = new StdClass();
59+
$foo->name = 'Mario';
60+
$foo->lambda = function ($text, $render) {
61+
return strtoupper($render($text));
62+
};
63+
64+
$this->assertEquals('Mario', $one->render($foo));
65+
$this->assertEquals('MARIO', $two->render($foo));
66+
}
3967
}

test/Mustache/Test/FiveThree/Functional/PartialLambdaIndentTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ public function testLambdasInsidePartialsAreIndentedProperly()
3333
<input placeholder="ENTER YOUR NAME">
3434
</fieldset>
3535
36+
EOS;
37+
38+
$m = new Mustache_Engine(array(
39+
'partials' => array('input' => $partial),
40+
));
41+
42+
$tpl = $m->loadTemplate($src);
43+
44+
$data = new Mustache_Test_FiveThree_Functional_ClassWithLambda();
45+
$this->assertEquals($expected, $tpl->render($data));
46+
}
47+
48+
public function testLambdaInterpolationsInsidePartialsAreIndentedProperly()
49+
{
50+
$src = <<<EOS
51+
<fieldset>
52+
{{> input }}
53+
</fieldset>
54+
55+
EOS;
56+
$partial = <<<EOS
57+
<input placeholder="{{ placeholder }}">
58+
59+
EOS;
60+
61+
$expected = <<<EOS
62+
<fieldset>
63+
<input placeholder="Enter your name">
64+
</fieldset>
65+
3666
EOS;
3767

3868
$m = new Mustache_Engine(array(
@@ -54,4 +84,11 @@ public function _t()
5484
return strtoupper($val);
5585
};
5686
}
87+
88+
public function placeholder()
89+
{
90+
return function () {
91+
return 'Enter your name';
92+
};
93+
}
5794
}

0 commit comments

Comments
 (0)