Pass a helper to section lambdas
This commit is contained in:
@@ -118,8 +118,11 @@ class Mustache_Compiler
|
||||
|
||||
class %s extends Mustache_Template
|
||||
{
|
||||
private $lambdaHelper;
|
||||
|
||||
public function renderInternal(Mustache_Context $context, $indent = \'\', $escape = false)
|
||||
{
|
||||
$this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context);
|
||||
$buffer = \'\';
|
||||
%s
|
||||
|
||||
@@ -159,7 +162,7 @@ class Mustache_Compiler
|
||||
if (!is_string($value) && is_callable($value)) {
|
||||
$source = %s;
|
||||
$buffer .= $this->mustache
|
||||
->loadLambda((string) call_user_func($value, $source)%s)
|
||||
->loadLambda((string) call_user_func($value, $source, $this->lambdaHelper)%s)
|
||||
->renderInternal($context, $indent);
|
||||
} elseif (!empty($value)) {
|
||||
$values = $this->isIterable($value) ? $value : array($value);
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Mustache.php.
|
||||
*
|
||||
* (c) 2012 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mustache Lambda Helper.
|
||||
*
|
||||
* Passed to section and interpolation lambdas, giving them access to a `render`
|
||||
* method for rendering a string with the current context.
|
||||
*/
|
||||
class Mustache_LambdaHelper
|
||||
{
|
||||
private $mustache;
|
||||
private $context;
|
||||
|
||||
/**
|
||||
* Mustache Lambda Helper constructor.
|
||||
*
|
||||
* @param Mustache_Engine $mustache Mustache engine instance.
|
||||
* @param Mustache_Context $context Rendering context.
|
||||
*/
|
||||
public function __construct(Mustache_Engine $mustache, Mustache_Context $context)
|
||||
{
|
||||
$this->mustache = $mustache;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a string as a Mustache template with the current rendering context.
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return Rendered template.
|
||||
*/
|
||||
public function render($string)
|
||||
{
|
||||
return $this->mustache
|
||||
->loadLambda((string) $string)
|
||||
->renderInternal($this->context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Mustache.php.
|
||||
*
|
||||
* (c) 2012 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group lambdas
|
||||
* @group functional
|
||||
*/
|
||||
class Mustache_Test_FiveThree_Functional_LambdaHelperTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
private $mustache;
|
||||
|
||||
public function setUp() {
|
||||
$this->mustache = new Mustache_Engine;
|
||||
}
|
||||
|
||||
public function testSectionLambdaHelper() {
|
||||
$one = $this->mustache->loadTemplate('{{name}}');
|
||||
$two = $this->mustache->loadTemplate('{{#lambda}}{{name}}{{/lambda}}');
|
||||
|
||||
$foo = new StdClass;
|
||||
$foo->name = 'Mario';
|
||||
$foo->lambda = function($text, $mustache) {
|
||||
return strtoupper($mustache->render($text));
|
||||
};
|
||||
|
||||
$this->assertEquals('Mario', $one->render($foo));
|
||||
$this->assertEquals('MARIO', $two->render($foo));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user