4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Loops over each item of a sequence.
18 * {% for user in users %}
19 * <li>{{ user.username|e }}</li>
26 class Twig_TokenParser_For extends Twig_TokenParser
28 public function parse(Twig_Token $token)
30 $lineno = $token->getLine();
31 $stream = $this->parser->getStream();
32 $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
33 $stream->expect(Twig_Token::OPERATOR_TYPE, 'in');
34 $seq = $this->parser->getExpressionParser()->parseExpression();
37 if ($stream->nextIf(Twig_Token::NAME_TYPE, 'if')) {
38 $ifexpr = $this->parser->getExpressionParser()->parseExpression();
41 $stream->expect(Twig_Token::BLOCK_END_TYPE);
42 $body = $this->parser->subparse(array($this, 'decideForFork'));
43 if ('else' == $stream->next()->getValue()) {
44 $stream->expect(Twig_Token::BLOCK_END_TYPE);
45 $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
49 $stream->expect(Twig_Token::BLOCK_END_TYPE);
51 if (count($targets) > 1) {
52 $keyTarget = $targets->getNode(0);
53 $keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
54 $valueTarget = $targets->getNode(1);
55 $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
57 $keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
58 $valueTarget = $targets->getNode(0);
59 $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
63 $this->checkLoopUsageCondition($stream, $ifexpr);
64 $this->checkLoopUsageBody($stream, $body);
67 return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
70 public function decideForFork(Twig_Token $token)
72 return $token->test(array('else', 'endfor'));
75 public function decideForEnd(Twig_Token $token)
77 return $token->test('endfor');
80 // the loop variable cannot be used in the condition
81 protected function checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
83 if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
84 throw new Twig_Error_Syntax('The "loop" variable cannot be used in a looping condition.', $node->getTemplateLine(), $stream->getSourceContext());
87 foreach ($node as $n) {
92 $this->checkLoopUsageCondition($stream, $n);
96 // check usage of non-defined loop-items
97 // it does not catch all problems (for instance when a for is included into another or when the variable is used in an include)
98 protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
100 if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
101 $attribute = $node->getNode('attribute');
102 if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) {
103 throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute->getAttribute('value')), $node->getTemplateLine(), $stream->getSourceContext());
107 // should check for parent.loop.XXX usage
108 if ($node instanceof Twig_Node_For) {
112 foreach ($node as $n) {
117 $this->checkLoopUsageBody($stream, $n);
121 public function getTag()
127 class_alias('Twig_TokenParser_For', 'Twig\TokenParser\ForTokenParser', false);