2ab0ea5d7c80ac2e71904c3377e1b76edd1f07aa
[yaffs-website] / vendor / twig / twig / lib / Twig / Node / With.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * Represents a nested "with" scope.
14  *
15  * @author Fabien Potencier <fabien@symfony.com>
16  */
17 class Twig_Node_With extends Twig_Node
18 {
19     public function __construct(Twig_Node $body, Twig_Node $variables = null, $only = false, $lineno, $tag = null)
20     {
21         $nodes = array('body' => $body);
22         if (null !== $variables) {
23             $nodes['variables'] = $variables;
24         }
25
26         parent::__construct($nodes, array('only' => (bool) $only), $lineno, $tag);
27     }
28
29     public function compile(Twig_Compiler $compiler)
30     {
31         $compiler->addDebugInfo($this);
32
33         if ($this->hasNode('variables')) {
34             $varsName = $compiler->getVarName();
35             $compiler
36                 ->write(sprintf('$%s = ', $varsName))
37                 ->subcompile($this->getNode('variables'))
38                 ->raw(";\n")
39                 ->write(sprintf("if (!is_array(\$%s)) {\n", $varsName))
40                 ->indent()
41                 ->write("throw new Twig_Error_Runtime('Variables passed to the \"with\" tag must be a hash.');\n")
42                 ->outdent()
43                 ->write("}\n")
44             ;
45
46             if ($this->getAttribute('only')) {
47                 $compiler->write("\$context = array('_parent' => \$context);\n");
48             } else {
49                 $compiler->write("\$context['_parent'] = \$context;\n");
50             }
51
52             $compiler->write(sprintf("\$context = array_merge(\$context, \$%s);\n", $varsName));
53         } else {
54             $compiler->write("\$context['_parent'] = \$context;\n");
55         }
56
57         $compiler
58             ->subcompile($this->getNode('body'))
59             ->write("\$context = \$context['_parent'];\n")
60         ;
61     }
62 }
63
64 class_alias('Twig_Node_With', 'Twig\Node\WithNode', false);