Respect delimiter changes in lambda helper.

See janl/mustache.js#489
This commit is contained in:
Justin Hileman
2015-10-12 12:48:21 -07:00
parent 5353b36ec6
commit 9cc21d63f7
3 changed files with 36 additions and 5 deletions
+6 -3
View File
@@ -328,7 +328,7 @@ class Mustache_Compiler
$buffer = \'\'; $buffer = \'\';
if (%s) { if (%s) {
$source = %s; $source = %s;
$result = call_user_func($value, $source, $this->lambdaHelper); $result = call_user_func($value, $source, %s);
if (strpos($result, \'{{\') === false) { if (strpos($result, \'{{\') === false) {
$buffer .= $result; $buffer .= $result;
} else { } else {
@@ -370,15 +370,18 @@ class Mustache_Compiler
$callable = $this->getCallable(); $callable = $this->getCallable();
if ($otag !== '{{' || $ctag !== '}}') { if ($otag !== '{{' || $ctag !== '}}') {
$delims = ', ' . var_export(sprintf('{{= %s %s =}}', $otag, $ctag), true); $delimTag = var_export(sprintf('{{= %s %s =}}', $otag, $ctag), true);
$helper = sprintf('$this->lambdaHelper->withDelimiters(%s)', $delimTag);
$delims = ', ' . $delimTag;
} else { } else {
$helper = '$this->lambdaHelper';
$delims = ''; $delims = '';
} }
$key = ucfirst(md5($delims . "\n" . $source)); $key = ucfirst(md5($delims . "\n" . $source));
if (!isset($this->sections[$key])) { if (!isset($this->sections[$key])) {
$this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $delims, $this->walk($nodes, 2)); $this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $helper, $delims, $this->walk($nodes, 2));
} }
if ($arg === true) { if ($arg === true) {
+17 -2
View File
@@ -20,17 +20,20 @@ class Mustache_LambdaHelper
{ {
private $mustache; private $mustache;
private $context; private $context;
private $delims;
/** /**
* Mustache Lambda Helper constructor. * Mustache Lambda Helper constructor.
* *
* @param Mustache_Engine $mustache Mustache engine instance. * @param Mustache_Engine $mustache Mustache engine instance.
* @param Mustache_Context $context Rendering context. * @param Mustache_Context $context Rendering context.
* @param string $delims Optional custom delimiters, in the format `{{= <% %> =}}`. (default: null)
*/ */
public function __construct(Mustache_Engine $mustache, Mustache_Context $context) public function __construct(Mustache_Engine $mustache, Mustache_Context $context, $delims = null)
{ {
$this->mustache = $mustache; $this->mustache = $mustache;
$this->context = $context; $this->context = $context;
$this->delims = $delims;
} }
/** /**
@@ -43,7 +46,19 @@ class Mustache_LambdaHelper
public function render($string) public function render($string)
{ {
return $this->mustache return $this->mustache
->loadLambda((string) $string) ->loadLambda((string) $string, $this->delims)
->renderInternal($this->context); ->renderInternal($this->context);
} }
/**
* Get a Lambda Helper with custom delimiters.
*
* @param string $delims Custom delimiters, in the format `{{= <% %> =}}`.
*
* @return Mustache_LambdaHelper
*/
public function withDelimiters($delims)
{
return new self($this->mustache, $this->context, $delims);
}
} }
@@ -36,4 +36,17 @@ class Mustache_Test_FiveThree_Functional_LambdaHelperTest extends PHPUnit_Framew
$this->assertEquals('Mario', $one->render($foo)); $this->assertEquals('Mario', $one->render($foo));
$this->assertEquals('MARIO', $two->render($foo)); $this->assertEquals('MARIO', $two->render($foo));
} }
public function testSectionLambdaHelperRespectsDelimiterChanges()
{
$tpl = $this->mustache->loadTemplate("{{=<% %>=}}\n<%# bang %><% value %><%/ bang %>");
$data = new StdClass();
$data->value = 'hello world';
$data->bang = function ($text, $mustache) {
return $mustache->render($text) . '!';
};
$this->assertEquals('hello world!', $tpl->render($data));
}
} }