Use Template->getContext in non-strict mode too

This commit is contained in:
nikic
2011-07-02 23:43:11 +02:00
parent 661e9c7ad2
commit a5ef32663b
2 changed files with 8 additions and 4 deletions
+1 -3
View File
@@ -34,10 +34,8 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
} }
} elseif (isset($specialVars[$name])) { } elseif (isset($specialVars[$name])) {
$compiler->raw($specialVars[$name]); $compiler->raw($specialVars[$name]);
} elseif ($compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name));
} else { } else {
$compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $name, $name)); $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name));
} }
} }
} }
+7 -1
View File
@@ -224,11 +224,17 @@ abstract class Twig_Template implements Twig_TemplateInterface
* @param array $context The context * @param array $context The context
* @param string $item The variable to return from the context * @param string $item The variable to return from the context
* *
* @throws Twig_Error_Runtime if the variable does not exist * @return The content of the context variable
*
* @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode
*/ */
protected function getContext($context, $item) protected function getContext($context, $item)
{ {
if (!array_key_exists($item, $context)) { if (!array_key_exists($item, $context)) {
if (!$this->env->isStrictVariables()) {
return null;
}
throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item)); throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
} }