1
|
<?php
|
2
|
|
3
|
/*
|
4
|
* This file is part of Twig.
|
5
|
*
|
6
|
* (c) 2009 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
|
|
12
|
/**
|
13
|
* Defines a variable.
|
14
|
*
|
15
|
* <pre>
|
16
|
* {% set foo = 'foo' %}
|
17
|
*
|
18
|
* {% set foo = [1, 2] %}
|
19
|
*
|
20
|
* {% set foo = {'foo': 'bar'} %}
|
21
|
*
|
22
|
* {% set foo = 'foo' ~ 'bar' %}
|
23
|
*
|
24
|
* {% set foo, bar = 'foo', 'bar' %}
|
25
|
*
|
26
|
* {% set foo %}Some content{% endset %}
|
27
|
* </pre>
|
28
|
*/
|
29
|
class Twig_TokenParser_Set extends Twig_TokenParser
|
30
|
{
|
31
|
public function parse(Twig_Token $token)
|
32
|
{
|
33
|
$lineno = $token->getLine();
|
34
|
$stream = $this->parser->getStream();
|
35
|
$names = $this->parser->getExpressionParser()->parseAssignmentExpression();
|
36
|
|
37
|
$capture = false;
|
38
|
if ($stream->nextIf(Twig_Token::OPERATOR_TYPE, '=')) {
|
39
|
$values = $this->parser->getExpressionParser()->parseMultitargetExpression();
|
40
|
|
41
|
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
42
|
|
43
|
if (count($names) !== count($values)) {
|
44
|
throw new Twig_Error_Syntax('When using set, you must have the same number of variables and assignments.', $stream->getCurrent()->getLine(), $stream->getFilename());
|
45
|
}
|
46
|
} else {
|
47
|
$capture = true;
|
48
|
|
49
|
if (count($names) > 1) {
|
50
|
throw new Twig_Error_Syntax('When using set with a block, you cannot have a multi-target.', $stream->getCurrent()->getLine(), $stream->getFilename());
|
51
|
}
|
52
|
|
53
|
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
54
|
|
55
|
$values = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
|
56
|
$stream->expect(Twig_Token::BLOCK_END_TYPE);
|
57
|
}
|
58
|
|
59
|
return new Twig_Node_Set($capture, $names, $values, $lineno, $this->getTag());
|
60
|
}
|
61
|
|
62
|
public function decideBlockEnd(Twig_Token $token)
|
63
|
{
|
64
|
return $token->test('endset');
|
65
|
}
|
66
|
|
67
|
public function getTag()
|
68
|
{
|
69
|
return 'set';
|
70
|
}
|
71
|
}
|