class Twig_Node_Expression_Test_Defined

Checks if a variable is defined in the current context.

<pre> {# defined works with variable names and variable attributes #} {% if foo is defined %} {# ... #} {% endif %} </pre>

@package twig @author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of Twig_Node_Expression_Test_Defined

1 string reference to 'Twig_Node_Expression_Test_Defined'
Twig_Extension_Core::getTests in drupal/core/vendor/twig/twig/lib/Twig/Extension/Core.php
Returns a list of tests to add to the existing list.

File

drupal/core/vendor/twig/twig/lib/Twig/Node/Expression/Test/Defined.php, line 25

View source
class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test {
  public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno) {
    parent::__construct($node, $name, $arguments, $lineno);
    if ($node instanceof Twig_Node_Expression_Name) {
      $node
        ->setAttribute('is_defined_test', true);
    }
    elseif ($node instanceof Twig_Node_Expression_GetAttr) {
      $node
        ->setAttribute('is_defined_test', true);
      $this
        ->changeIgnoreStrictCheck($node);
    }
    else {
      throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this
        ->getLine());
    }
  }
  protected function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node) {
    $node
      ->setAttribute('ignore_strict_check', true);
    if ($node
      ->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
      $this
        ->changeIgnoreStrictCheck($node
        ->getNode('node'));
    }
  }
  public function compile(Twig_Compiler $compiler) {
    $compiler
      ->subcompile($this
      ->getNode('node'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Twig_Node::$attributes protected property
Twig_Node::$lineno protected property
Twig_Node::$nodes protected property
Twig_Node::$tag protected property
Twig_Node::count public function
Twig_Node::getAttribute public function Gets an attribute.
Twig_Node::getIterator public function
Twig_Node::getLine public function Overrides Twig_NodeInterface::getLine
Twig_Node::getNode public function Gets a node by name.
Twig_Node::getNodeTag public function Overrides Twig_NodeInterface::getNodeTag
Twig_Node::hasAttribute public function Returns true if the attribute is defined.
Twig_Node::hasNode public function Returns true if the node with the given identifier exists.
Twig_Node::removeAttribute public function Removes an attribute.
Twig_Node::removeNode public function Removes a node by name.
Twig_Node::setAttribute public function Sets an attribute.
Twig_Node::setNode public function Sets a node.
Twig_Node::toXml public function
Twig_Node::__toString public function
Twig_Node_Expression_Test_Defined::changeIgnoreStrictCheck protected function
Twig_Node_Expression_Test_Defined::compile public function Compiles the node to PHP. Overrides Twig_Node_Expression_Test::compile
Twig_Node_Expression_Test_Defined::__construct public function Constructor. Overrides Twig_Node_Expression_Test::__construct