Twig-1.3.0/lib/Twig/Node/Expression/Name.php
changeset 4 9a001a04b634
equal deleted inserted replaced
3:6d109e3804ac 4:9a001a04b634
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of Twig.
       
     5  *
       
     6  * (c) 2009 Fabien Potencier
       
     7  * (c) 2009 Armin Ronacher
       
     8  *
       
     9  * For the full copyright and license information, please view the LICENSE
       
    10  * file that was distributed with this source code.
       
    11  */
       
    12 class Twig_Node_Expression_Name extends Twig_Node_Expression
       
    13 {
       
    14     public function __construct($name, $lineno)
       
    15     {
       
    16         parent::__construct(array(), array('name' => $name, 'output' => false), $lineno);
       
    17     }
       
    18 
       
    19     public function compile(Twig_Compiler $compiler)
       
    20     {
       
    21         static $specialVars = array(
       
    22             '_self'    => '$this',
       
    23             '_context' => '$context',
       
    24             '_charset' => '$this->env->getCharset()',
       
    25         );
       
    26 
       
    27         $name = $this->getAttribute('name');
       
    28 
       
    29         if ($this->hasAttribute('is_defined_test')) {
       
    30             if (isset($specialVars[$name])) {
       
    31                 $compiler->repr(true);
       
    32             } else {
       
    33                 $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
       
    34             }
       
    35         } elseif (isset($specialVars[$name])) {
       
    36             $compiler->raw($specialVars[$name]);
       
    37         } elseif ($this->getAttribute('output')) {
       
    38             $compiler
       
    39                 ->addDebugInfo($this)
       
    40                 ->write('if (isset($context[')
       
    41                 ->string($name)
       
    42                 ->raw("])) {\n")
       
    43                 ->indent()
       
    44                 ->write('echo $context[')
       
    45                 ->string($name)
       
    46                 ->raw("];\n")
       
    47                 ->outdent()
       
    48                 ->write("}\n")
       
    49             ;
       
    50         } else {
       
    51             $compiler
       
    52                 ->raw('$this->getContext($context, ')
       
    53                 ->string($name)
       
    54                 ->raw(')')
       
    55             ;
       
    56         }
       
    57     }
       
    58 }