// defined is a special case
if ('defined' === $name) {
- if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) {
+ if ($node instanceof Twig_Node_Expression_Name) {
$node->setAttribute('is_defined_test', true);
+ } elseif ($node instanceof Twig_Node_Expression_GetAttr) {
+ $this->changeIsDefined($node);
} else {
throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
}
}
}
+ protected function changeIsDefined(Twig_Node_Expression_GetAttr $node)
+ {
+ $node->setAttribute('is_defined_test', true);
+
+ if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
+ $this->changeIsDefined($node->getNode('node'));
+ }
+ }
+
public function compile(Twig_Compiler $compiler)
{
$testMap = $compiler->getEnvironment()->getTests();
{{ 'o' ~ nullVar|default('k') }}
{{ 'o' ~ nested.nullVar|default('k') }}
Deep nested:
-{{ nested.definedVar.undefinedVar.foo.bar|default('default') is sameas('default') ? 'ok' : 'ko' }}
+{{ nested.undefinedVar.foo.bar|default('default') is sameas('default') ? 'ok' : 'ko' }}
--DATA--
return array(
'definedVar' => 'defined',
ok
Deep nested:
ok
+--DATA--
+return array(
+ 'definedVar' => 'defined',
+ 'zeroVar' => 0,
+ 'emptyVar' => '',
+ 'nullVar' => null,
+ 'nested' => array(
+ 'definedVar' => 'defined',
+ 'zeroVar' => 0,
+ 'emptyVar' => '',
+ 'nullVar' => null,
+ ),
+)
+--CONFIG--
+return array('strict_variables' => true)
+--EXPECT--
+Variable:
+ok
+ok
+ok
+ok
+ok
+Array access:
+ok
+ok
+ok
+ok
+ok
+ok
+Plain values:
+ok
+ok
+ok
+ok
+Precedence:
+ok
+ok
+Deep nested:
+ok