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 set node.
15 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Node_Set extends Twig_Node implements Twig_NodeCaptureInterface
19 public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null)
21 parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture, 'safe' => false), $lineno, $tag);
24 * Optimizes the node when capture is used for a large block of text.
26 * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
28 if ($this->getAttribute('capture')) {
29 $this->setAttribute('safe', true);
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);
39 public function compile(Twig_Compiler $compiler)
41 $compiler->addDebugInfo($this);
43 if (count($this->getNode('names')) > 1) {
44 $compiler->write('list(');
45 foreach ($this->getNode('names') as $idx => $node) {
50 $compiler->subcompile($node);
54 if ($this->getAttribute('capture')) {
56 ->write("ob_start();\n")
57 ->subcompile($this->getNode('values'))
61 $compiler->subcompile($this->getNode('names'), false);
63 if ($this->getAttribute('capture')) {
64 $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())");
68 if (!$this->getAttribute('capture')) {
69 $compiler->raw(' = ');
71 if (count($this->getNode('names')) > 1) {
72 $compiler->write('array(');
73 foreach ($this->getNode('values') as $idx => $value) {
78 $compiler->subcompile($value);
82 if ($this->getAttribute('safe')) {
84 ->raw("('' === \$tmp = ")
85 ->subcompile($this->getNode('values'))
86 ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())")
89 $compiler->subcompile($this->getNode('values'));
94 $compiler->raw(";\n");
98 class_alias('Twig_Node_Set', 'Twig\Node\SetNode', false);