ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Set.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($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null)
20 {
21 parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture, 'safe' => false), $lineno, $tag);
22
23 /*
24 * Optimizes the node when capture is used for a large block of text.
25 *
26 * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
27 */
28 if ($this->getAttribute('capture')) {
29 $this->setAttribute('safe', true);
30
31 $values = $this->getNode('values');
32 if ($values instanceof Twig_Node_Text) {
33 $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getTemplateLine()));
34 $this->setAttribute('capture', false);
35 }
36 }
37 }
38
39 public function compile(Twig_Compiler $compiler)
40 {
41 $compiler->addDebugInfo($this);
42
43 if (count($this->getNode('names')) > 1) {
44 $compiler->write('list(');
45 foreach ($this->getNode('names') as $idx => $node) {
46 if ($idx) {
47 $compiler->raw(', ');
48 }
49
50 $compiler->subcompile($node);
51 }
52 $compiler->raw(')');
53 } else {
54 if ($this->getAttribute('capture')) {
55 $compiler
56 ->write("ob_start();\n")
57 ->subcompile($this->getNode('values'))
58 ;
59 }
60
61 $compiler->subcompile($this->getNode('names'), false);
62
63 if ($this->getAttribute('capture')) {
64 $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())");
65 }
66 }
67
68 if (!$this->getAttribute('capture')) {
69 $compiler->raw(' = ');
70
71 if (count($this->getNode('names')) > 1) {
72 $compiler->write('array(');
73 foreach ($this->getNode('values') as $idx => $value) {
74 if ($idx) {
75 $compiler->raw(', ');
76 }
77
78 $compiler->subcompile($value);
79 }
80 $compiler->raw(')');
81 } else {
82 if ($this->getAttribute('safe')) {
83 $compiler
84 ->raw("('' === \$tmp = ")
85 ->subcompile($this->getNode('values'))
86 ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())")
87 ;
88 } else {
89 $compiler->subcompile($this->getNode('values'));
90 }
91 }
92 }
93
94 $compiler->raw(";\n");
95 }
96}
97
98class_alias('Twig_Node_Set', 'Twig\Node\SetNode', false);
An exception for terminatinating execution or to throw for unit testing.
Compiles a node to PHP code.
Definition: Compiler.php:19
raw($string)
Adds a raw string to the compiled code.
Definition: Compiler.php:112
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 set node.
Definition: Set.php:18
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
Definition: Set.php:39
__construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag=null)
Definition: Set.php:19
Represents a text node.
Definition: Text.php:19
Represents a node in the AST.
Definition: Node.php:19
setNode($name, $node=null)
Definition: Node.php:195
getAttribute($name)
Definition: Node.php:152
$lineno
Definition: Node.php:22
getNode($name)
Definition: Node.php:186
setAttribute($name, $value)
Definition: Node.php:165
count()
Definition: Node.php:209
Represents a node that captures any nested displayable nodes.
Represents a node in the AST.
$values