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.
16 * {% set foo = 'foo' %}
18 * {% set foo = [1, 2] %}
20 * {% set foo = {'foo': 'bar'} %}
22 * {% set foo = 'foo' ~ 'bar' %}
24 * {% set foo, bar = 'foo', 'bar' %}
26 * {% set foo %}Some content{% endset %}
31 class Twig_TokenParser_Set extends Twig_TokenParser
33 public function parse(Twig_Token $token)
35 $lineno = $token->getLine();
36 $stream = $this->parser->getStream();
37 $names = $this->parser->getExpressionParser()->parseAssignmentExpression();
40 if ($stream->nextIf(Twig_Token::OPERATOR_TYPE, '=')) {
41 $values = $this->parser->getExpressionParser()->parseMultitargetExpression();
43 $stream->expect(Twig_Token::BLOCK_END_TYPE);
45 if (count($names) !== count($values)) {
46 throw new Twig_Error_Syntax('When using set, you must have the same number of variables and assignments.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
51 if (count($names) > 1) {
52 throw new Twig_Error_Syntax('When using set with a block, you cannot have a multi-target.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
55 $stream->expect(Twig_Token::BLOCK_END_TYPE);
57 $values = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
58 $stream->expect(Twig_Token::BLOCK_END_TYPE);
61 return new Twig_Node_Set($capture, $names, $values, $lineno, $this->getTag());
64 public function decideBlockEnd(Twig_Token $token)
66 return $token->test('endset');
69 public function getTag()
75 class_alias('Twig_TokenParser_Set', 'Twig\TokenParser\SetTokenParser', false);