Permalink
Browse files

Add `resolveValue` method to base Template.

DRYs up compiled class output a bit and makes things FASTER!

Speeds up value resolution by 33% for primitives, 25% for methods and properties, and 10% for lambdas.
  • Loading branch information...
1 parent cf76a4b commit a777c587f2349c8ee09a48d3b0d6f4a6757e6d84 @bobthecow committed Jan 26, 2013
Showing with 25 additions and 8 deletions.
  1. +1 −6 src/Mustache/Compiler.php
  2. +22 −0 src/Mustache/Template.php
  3. +2 −2 test/Mustache/Test/CompilerTest.php
@@ -275,12 +275,7 @@ private function partial($id, $indent, $level)
}
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;
';
View
@@ -147,4 +147,26 @@ protected function prepareContextStack($context = null)
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;
+ }
}
@@ -70,9 +70,9 @@ public function getCompileValues()
"\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;',
)

0 comments on commit a777c58

Please sign in to comment.