ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
With.php
Go to the documentation of this file.
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
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
64class_alias('Twig_Node_With', 'Twig\Node\WithNode', false);
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
Compiles a node to PHP code.
Definition: Compiler.php:19
addDebugInfo(Twig_NodeInterface $node)
Adds debugging information.
Definition: Compiler.php:212
subcompile(Twig_NodeInterface $node, $raw=true)
Definition: Compiler.php:94
write()
Writes a string to the compiled code by adding indentation.
Definition: Compiler.php:124
Represents a nested "with" scope.
Definition: With.php:18
__construct(Twig_Node $body, Twig_Node $variables=null, $only=false, $lineno, $tag=null)
Definition: With.php:19
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
Definition: With.php:29
Represents a node in the AST.
Definition: Node.php:19
hasNode($name)
Definition: Node.php:178
getAttribute($name)
Definition: Node.php:152
$lineno
Definition: Node.php:22
getNode($name)
Definition: Node.php:186
$nodes
Definition: Node.php:20