Merge branch 'release/2.10.0'

This commit is contained in:
Justin Hileman
2016-02-27 11:22:46 -08:00
8 changed files with 110 additions and 16 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
"php": ">=5.2.4" "php": ">=5.2.4"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~3.7|~4.0", "phpunit/phpunit": "~3.7|~4.0|~5.0",
"fabpot/php-cs-fixer": "~1.6" "fabpot/php-cs-fixer": "~1.6"
}, },
"autoload": { "autoload": {
+7 -4
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) {
@@ -495,7 +498,7 @@ class Mustache_Compiler
} }
const VARIABLE = ' const VARIABLE = '
$value = $this->resolveValue($context->%s(%s), $context, $indent);%s $value = $this->resolveValue($context->%s(%s), $context);%s
$buffer .= %s%s; $buffer .= %s%s;
'; ';
+1 -1
View File
@@ -23,7 +23,7 @@
*/ */
class Mustache_Engine class Mustache_Engine
{ {
const VERSION = '2.9.0'; const VERSION = '2.10.0';
const SPEC_VERSION = '1.1.2'; const SPEC_VERSION = '1.1.2';
const PRAGMA_FILTERS = 'FILTERS'; const PRAGMA_FILTERS = 'FILTERS';
+29 -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,31 @@ 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);
} }
/**
* Render a string as a Mustache template with the current rendering context.
*
* @param string $string
*
* @return string Rendered template
*/
public function __invoke($string)
{
return $this->render($string);
}
/**
* 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);
}
} }
+2 -3
View File
@@ -164,16 +164,15 @@ abstract class Mustache_Template
* *
* @param mixed $value * @param mixed $value
* @param Mustache_Context $context * @param Mustache_Context $context
* @param string $indent
* *
* @return string * @return string
*/ */
protected function resolveValue($value, Mustache_Context $context, $indent = '') protected function resolveValue($value, Mustache_Context $context)
{ {
if (($this->strictCallables ? is_object($value) : !is_string($value)) && is_callable($value)) { if (($this->strictCallables ? is_object($value) : !is_string($value)) && is_callable($value)) {
return $this->mustache return $this->mustache
->loadLambda((string) call_user_func($value)) ->loadLambda((string) call_user_func($value))
->renderInternal($context, $indent); ->renderInternal($context);
} }
return $value; return $value;
+5 -5
View File
@@ -55,7 +55,7 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
'ISO-8859-1', 'ISO-8859-1',
array( array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);', '$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= $indent . call_user_func($this->mustache->getEscape(), $value);', '$buffer .= $indent . call_user_func($this->mustache->getEscape(), $value);',
'return $buffer;', 'return $buffer;',
), ),
@@ -75,7 +75,7 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
'ISO-8859-1', 'ISO-8859-1',
array( array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);', '$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_COMPAT . ', \'ISO-8859-1\');', '$buffer .= $indent . htmlspecialchars($value, ' . ENT_COMPAT . ', \'ISO-8859-1\');',
'return $buffer;', 'return $buffer;',
), ),
@@ -95,7 +95,7 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
'ISO-8859-1', 'ISO-8859-1',
array( array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);', '$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_QUOTES . ', \'ISO-8859-1\');', '$buffer .= $indent . htmlspecialchars($value, ' . ENT_QUOTES . ', \'ISO-8859-1\');',
'return $buffer;', 'return $buffer;',
), ),
@@ -122,9 +122,9 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase
array( array(
"\nclass Monkey extends Mustache_Template", "\nclass Monkey extends Mustache_Template",
"\$buffer .= \$indent . 'foo\n';", "\$buffer .= \$indent . 'foo\n';",
'$value = $this->resolveValue($context->find(\'name\'), $context, $indent);', '$value = $this->resolveValue($context->find(\'name\'), $context);',
'$buffer .= htmlspecialchars($value, ' . ENT_COMPAT . ', \'UTF-8\');', '$buffer .= htmlspecialchars($value, ' . ENT_COMPAT . ', \'UTF-8\');',
'$value = $this->resolveValue($context->last(), $context, $indent);', '$value = $this->resolveValue($context->last(), $context);',
'$buffer .= \'\\\'bar\\\'\';', '$buffer .= \'\\\'bar\\\'\';',
'return $buffer;', 'return $buffer;',
), ),
@@ -36,4 +36,32 @@ 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));
}
public function testLambdaHelperIsInvokable()
{
$one = $this->mustache->loadTemplate('{{name}}');
$two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}');
$foo = new StdClass();
$foo->name = 'Mario';
$foo->lambda = function ($text, $render) {
return strtoupper($render($text));
};
$this->assertEquals('Mario', $one->render($foo));
$this->assertEquals('MARIO', $two->render($foo));
}
} }
@@ -33,6 +33,36 @@ EOS;
<input placeholder="ENTER YOUR NAME"> <input placeholder="ENTER YOUR NAME">
</fieldset> </fieldset>
EOS;
$m = new Mustache_Engine(array(
'partials' => array('input' => $partial),
));
$tpl = $m->loadTemplate($src);
$data = new Mustache_Test_FiveThree_Functional_ClassWithLambda();
$this->assertEquals($expected, $tpl->render($data));
}
public function testLambdaInterpolationsInsidePartialsAreIndentedProperly()
{
$src = <<<EOS
<fieldset>
{{> input }}
</fieldset>
EOS;
$partial = <<<EOS
<input placeholder="{{ placeholder }}">
EOS;
$expected = <<<EOS
<fieldset>
<input placeholder="Enter your name">
</fieldset>
EOS; EOS;
$m = new Mustache_Engine(array( $m = new Mustache_Engine(array(
@@ -54,4 +84,11 @@ class Mustache_Test_FiveThree_Functional_ClassWithLambda
return strtoupper($val); return strtoupper($val);
}; };
} }
public function placeholder()
{
return function () {
return 'Enter your name';
};
}
} }