diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index e35f6f7..27f02f0 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -275,12 +275,7 @@ class Mustache_Compiler } const VARIABLE = ' - $value = $context->%s(%s); - if (!is_string($value) && is_callable($value)) { - $value = $this->mustache - ->loadLambda((string) call_user_func($value)) - ->renderInternal($context, $indent); - }%s + $value = $this->resolveValue($context->%s(%s), $context, $indent);%s $buffer .= %s%s; '; diff --git a/src/Mustache/Template.php b/src/Mustache/Template.php index b3d57ed..4c67445 100644 --- a/src/Mustache/Template.php +++ b/src/Mustache/Template.php @@ -147,4 +147,26 @@ abstract class Mustache_Template return $stack; } + + /** + * Resolve a context value. + * + * Invoke the value if it is callable, otherwise return the value. + * + * @param mixed $value + * @param Mustache_Context $context + * @param string $indent + * + * @return string + */ + protected function resolveValue($value, Mustache_Context $context, $indent = '') + { + if (!is_string($value) && is_callable($value)) { + return $this->mustache + ->loadLambda((string) call_user_func($value)) + ->renderInternal($context, $indent); + } + + return $value; + } } diff --git a/test/Mustache/Test/CompilerTest.php b/test/Mustache/Test/CompilerTest.php index a94a3f3..134456d 100644 --- a/test/Mustache/Test/CompilerTest.php +++ b/test/Mustache/Test/CompilerTest.php @@ -70,9 +70,9 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase "\nclass Monkey extends Mustache_Template", '$buffer .= $indent . \'foo\'', '$buffer .= "\n"', - '$value = $context->find(\'name\');', + '$value = $this->resolveValue($context->find(\'name\'), $context, $indent);', '$buffer .= htmlspecialchars($value, ENT_COMPAT, \'UTF-8\');', - '$value = $context->last();', + '$value = $this->resolveValue($context->last(), $context, $indent);', '$buffer .= \'\\\'bar\\\'\';', 'return $buffer;', )