Project

General

Profile

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

    
13
/**
14
 * Represents an if node.
15
 *
16
 * @author Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_If extends Twig_Node
19
{
20
    public function __construct(Twig_NodeInterface $tests, Twig_NodeInterface $else = null, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('tests' => $tests, 'else' => $else), array(), $lineno, $tag);
23
    }
24

    
25
    public function compile(Twig_Compiler $compiler)
26
    {
27
        $compiler->addDebugInfo($this);
28
        for ($i = 0, $count = count($this->getNode('tests')); $i < $count; $i += 2) {
29
            if ($i > 0) {
30
                $compiler
31
                    ->outdent()
32
                    ->write('} elseif (')
33
                ;
34
            } else {
35
                $compiler
36
                    ->write('if (')
37
                ;
38
            }
39

    
40
            $compiler
41
                ->subcompile($this->getNode('tests')->getNode($i))
42
                ->raw(") {\n")
43
                ->indent()
44
                ->subcompile($this->getNode('tests')->getNode($i + 1))
45
            ;
46
        }
47

    
48
        if ($this->hasNode('else') && null !== $this->getNode('else')) {
49
            $compiler
50
                ->outdent()
51
                ->write("} else {\n")
52
                ->indent()
53
                ->subcompile($this->getNode('else'))
54
            ;
55
        }
56

    
57
        $compiler
58
            ->outdent()
59
            ->write("}\n");
60
    }
61
}
(12-12/23)