4 * This file is part of Twig.
6 * (c) 2011 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Returns the value or the default value when it is undefined or empty.
16 * {{ var.foo|default('foo item on var is not defined') }}
19 * @author Fabien Potencier <fabien@symfony.com>
21 class Twig_Node_Expression_Filter_Default
extends Twig_Node_Expression_Filter
23 public function __construct(Twig_NodeInterface
$node, Twig_Node_Expression_Constant
$filterName, Twig_NodeInterface
$arguments, $lineno, $tag = null)
25 $default = new Twig_Node_Expression_Filter($node, new Twig_Node_Expression_Constant('default', $node->getLine()), $arguments, $node->getLine());
27 if ('default' === $filterName->getAttribute('value') && ($node instanceof Twig_Node_Expression_Name
|| $node instanceof Twig_Node_Expression_GetAttr
)) {
28 $test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getLine());
29 $false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine());
31 $node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getLine());
36 parent
::__construct($node, $filterName, $arguments, $lineno, $tag);
39 public function compile(Twig_Compiler
$compiler)
41 $compiler->subcompile($this->getNode('node'));