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 
64 class_alias('Twig_Node_With', 'Twig\Node\WithNode', false);
$lineno
Definition: Node.php:22
subcompile(Twig_NodeInterface $node, $raw=true)
Definition: Compiler.php:94
Represents a node in the AST.
Definition: Node.php:18
$nodes
Definition: Node.php:20
getAttribute($name)
Definition: Node.php:152
__construct(Twig_Node $body, Twig_Node $variables=null, $only=false, $lineno, $tag=null)
Definition: With.php:19
hasNode($name)
Definition: Node.php:178
Create styles array
The data for the language used.
getNode($name)
Definition: Node.php:186
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
Definition: With.php:29
addDebugInfo(Twig_NodeInterface $node)
Adds debugging information.
Definition: Compiler.php:212
Represents a nested "with" scope.
Definition: With.php:17
write()
Writes a string to the compiled code by adding indentation.
Definition: Compiler.php:124