From a5ef32663b9fb9ba16734def7221bbaf33b7d904 Mon Sep 17 00:00:00 2001 From: nikic Date: Sat, 2 Jul 2011 23:43:11 +0200 Subject: [PATCH] Use Template->getContext in non-strict mode too --- lib/Twig/Node/Expression/Name.php | 4 +--- lib/Twig/Template.php | 8 +++++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index f7d1875..fceda2b 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -34,10 +34,8 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression } } elseif (isset($specialVars[$name])) { $compiler->raw($specialVars[$name]); - } elseif ($compiler->getEnvironment()->isStrictVariables()) { - $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name)); } else { - $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $name, $name)); + $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name)); } } } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index aaef78a..f4ff59e 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -224,11 +224,17 @@ abstract class Twig_Template implements Twig_TemplateInterface * @param array $context 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) { if (!array_key_exists($item, $context)) { + if (!$this->env->isStrictVariables()) { + return null; + } + throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item)); } -- 1.7.2.5