1
|
<?php
|
2
|
|
3
|
/*
|
4
|
* This file is part of Twig.
|
5
|
*
|
6
|
* (c) 2009 Fabien Potencier
|
7
|
* (c) 2009 Armin Ronacher
|
8
|
*
|
9
|
* For the full copyright and license information, please view the LICENSE
|
10
|
* file that was distributed with this source code.
|
11
|
*/
|
12
|
|
13
|
/**
|
14
|
* Loops over each item of a sequence.
|
15
|
*
|
16
|
* <pre>
|
17
|
* <ul>
|
18
|
* {% for user in users %}
|
19
|
* <li>{{ user.username|e }}</li>
|
20
|
* {% endfor %}
|
21
|
* </ul>
|
22
|
* </pre>
|
23
|
*/
|
24
|
class Twig_TokenParser_For extends Twig_TokenParser
|
25
|
{
|
26
|
/**
|
27
|
* Parses a token and returns a node.
|
28
|
*
|
29
|
* @param Twig_Token $token A Twig_Token instance
|
30
|
*
|
31
|
* @return Twig_NodeInterface A Twig_NodeInterface instance
|
32
|
*/
|
33
|
public function parse(Twig_Token $token)
|
34
|
{
|
35
|
$lineno = $token->getLine();
|
36
|
$targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
|
37
|
$this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, 'in');
|
38
|
$seq = $this->parser->getExpressionParser()->parseExpression();
|
39
|
|
40
|
$ifexpr = null;
|
41
|
if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'if')) {
|
42
|
$this->parser->getStream()->next();
|
43
|
$ifexpr = $this->parser->getExpressionParser()->parseExpression();
|
44
|
}
|
45
|
|
46
|
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
|
47
|
$body = $this->parser->subparse(array($this, 'decideForFork'));
|
48
|
if ($this->parser->getStream()->next()->getValue() == 'else') {
|
49
|
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
|
50
|
$else = $this->parser->subparse(array($this, 'decideForEnd'), true);
|
51
|
} else {
|
52
|
$else = null;
|
53
|
}
|
54
|
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
|
55
|
|
56
|
if (count($targets) > 1) {
|
57
|
$keyTarget = $targets->getNode(0);
|
58
|
$keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine());
|
59
|
$valueTarget = $targets->getNode(1);
|
60
|
$valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
|
61
|
} else {
|
62
|
$keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
|
63
|
$valueTarget = $targets->getNode(0);
|
64
|
$valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
|
65
|
}
|
66
|
|
67
|
return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
|
68
|
}
|
69
|
|
70
|
public function decideForFork(Twig_Token $token)
|
71
|
{
|
72
|
return $token->test(array('else', 'endfor'));
|
73
|
}
|
74
|
|
75
|
public function decideForEnd(Twig_Token $token)
|
76
|
{
|
77
|
return $token->test('endfor');
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Gets the tag name associated with this token parser.
|
82
|
*
|
83
|
* @return string The tag name
|
84
|
*/
|
85
|
public function getTag()
|
86
|
{
|
87
|
return 'for';
|
88
|
}
|
89
|
}
|