4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Represents a nested "with" scope.
15 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Node_With extends Twig_Node
19 public function __construct(Twig_Node $body, Twig_Node $variables = null, $only = false, $lineno, $tag = null)
21 $nodes = array('body' => $body);
22 if (null !== $variables) {
23 $nodes['variables'] = $variables;
26 parent::__construct($nodes, array('only' => (bool) $only), $lineno, $tag);
29 public function compile(Twig_Compiler $compiler)
31 $compiler->addDebugInfo($this);
33 if ($this->hasNode('variables')) {
34 $varsName = $compiler->getVarName();
36 ->write(sprintf('$%s = ', $varsName))
37 ->subcompile($this->getNode('variables'))
39 ->write(sprintf("if (!is_array(\$%s)) {\n", $varsName))
41 ->write("throw new Twig_Error_Runtime('Variables passed to the \"with\" tag must be a hash.');\n")
46 if ($this->getAttribute('only')) {
47 $compiler->write("\$context = array('_parent' => \$context);\n");
49 $compiler->write("\$context['_parent'] = \$context;\n");
52 $compiler->write(sprintf("\$context = array_merge(\$context, \$%s);\n", $varsName));
54 $compiler->write("\$context['_parent'] = \$context;\n");
58 ->subcompile($this->getNode('body'))
59 ->write("\$context = \$context['_parent'];\n")
64 class_alias('Twig_Node_With', 'Twig\Node\WithNode', false);