{
$compiler->raw('$this->getAttribute(');
- if ($this->getAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) {
- $compiler->subcompile(new Twig_Node_Expression_Filter(
- $this->getNode('node'),
- new Twig_Node_Expression_Constant('default', $this->getLine()),
- new Twig_Node(),
- $this->getLine()
- ));
- } else {
- $compiler->subcompile($this->getNode('node'));
+ if ($this->getAttribute('is_defined_test')) {
+ $this->getNode('node')->setAttribute('ignore_strict_check', true);
}
+ $compiler->subcompile($this->getNode('node'));
+
$compiler->raw(', ')->subcompile($this->getNode('attribute'));
if (count($this->getNode('arguments')) || Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test')) {
public function __construct($name, $lineno)
{
- parent::__construct(array(), array('name' => $name, 'is_defined_test' => false), $lineno);
+ parent::__construct(array(), array('name' => $name, 'is_defined_test' => false, 'ignore_strict_check' => false), $lineno);
}
public function compile(Twig_Compiler $compiler)
$compiler
->raw('$this->getContext($context, ')
->string($name)
+ ;
+
+ if ($this->getAttribute('ignore_strict_check')) {
+ $compiler->raw(', true');
+ }
+
+ $compiler
->raw(')')
;
}
/**
* Returns a variable from the context.
*
- * @param array $context The context
- * @param string $item The variable to return from the context
+ * @param array $context The context
+ * @param string $item The variable to return from the context
+ * @param Boolean $isDefinedTest Whether to ignore the strict variable check or not
*
* @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, $ignoreStrictCheck = false)
{
if (!array_key_exists($item, $context)) {
- if (!$this->env->isStrictVariables()) {
+ if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
return null;
}