return array(
'even' => new Twig_Test_Function('twig_test_even'),
'odd' => new Twig_Test_Function('twig_test_odd'),
- 'defined' => new Twig_Test_Function('twig_test_defined'),
+ 'defined' => new Twig_Test_Node('Twig_Node_Expression_Test_Defined'),
'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'),
'none' => new Twig_Test_Function('twig_test_none'),
'null' => new Twig_Test_Function('twig_test_none'),
}
/**
- * Checks if a variable is defined in the current context.
- *
- * <pre>
- * {# defined works with variable names #}
- * {% if foo is defined %}
- * {# ... #}
- * {% endif %}
- * </pre>
- *
- * @param mixed $name A PHP variable
- * @param array $context The current context
- *
- * @return Boolean true if the value is defined, false otherwise
- */
-function twig_test_defined($name, $context)
-{
- return array_key_exists($name, $context);
-}
-
-/**
* Checks if a variable is empty.
*
* <pre>
throw new Twig_Error('The default filter node cannot be created from the given node.', $node->getLine());
}
- $test = new Twig_Node_Expression_Test(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine());
+ $test = new Twig_Node_Expression_Test_Defined(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine());
$default = count($node->getNode('arguments')) ? $node->getNode('arguments')->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine());
$node = new Twig_Node_Expression_Conditional($test, $node, $default, $node->getLine());
public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
{
parent::__construct(array('node' => $node, 'arguments' => $arguments), array('name' => $name), $lineno);
-
- // defined is a special case
- if ('defined' === $name) {
- 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)
$name = $this->getAttribute('name');
$node = $this->getNode('node');
- // defined is a special case
- if ('defined' === $name) {
- $compiler->subcompile($node);
-
- return;
- }
-
$compiler
->raw($testMap[$name]->compile().'(')
->subcompile($node)
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2011 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * 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>
+ */
+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'));
+ }
+}