ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
For.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 * (c) 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
27{
28 public function parse(Twig_Token $token)
29 {
30 $lineno = $token->getLine();
31 $stream = $this->parser->getStream();
32 $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
34 $seq = $this->parser->getExpressionParser()->parseExpression();
35
36 $ifexpr = null;
37 if ($stream->nextIf(Twig_Token::NAME_TYPE, 'if')) {
38 $ifexpr = $this->parser->getExpressionParser()->parseExpression();
39 }
40
42 $body = $this->parser->subparse(array($this, 'decideForFork'));
43 if ('else' == $stream->next()->getValue()) {
45 $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
46 } else {
47 $else = null;
48 }
50
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());
56 } else {
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());
60 }
61
62 if ($ifexpr) {
63 $this->checkLoopUsageCondition($stream, $ifexpr);
64 $this->checkLoopUsageBody($stream, $body);
65 }
66
67 return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
68 }
69
71 {
72 return $token->test(array('else', 'endfor'));
73 }
74
76 {
77 return $token->test('endfor');
78 }
79
80 // the loop variable cannot be used in the condition
82 {
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());
85 }
86
87 foreach ($node as $n) {
88 if (!$n) {
89 continue;
90 }
91
92 $this->checkLoopUsageCondition($stream, $n);
93 }
94 }
95
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)
99 {
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());
104 }
105 }
106
107 // should check for parent.loop.XXX usage
108 if ($node instanceof Twig_Node_For) {
109 return;
110 }
111
112 foreach ($node as $n) {
113 if (!$n) {
114 continue;
115 }
116
117 $this->checkLoopUsageBody($stream, $n);
118 }
119 }
120
121 public function getTag()
122 {
123 return 'for';
124 }
125}
126
127class_alias('Twig_TokenParser_For', 'Twig\TokenParser\ForTokenParser', false);
$n
Definition: RandomTest.php:85
An exception for terminatinating execution or to throw for unit testing.
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:19
Represents a for node.
Definition: For.php:19
Loops over each item of a sequence.
Definition: For.php:27
checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:81
getTag()
Gets the tag name associated with this token parser.
Definition: For.php:121
checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:98
parse(Twig_Token $token)
Parses a token and returns a node.
Definition: For.php:28
decideForFork(Twig_Token $token)
Definition: For.php:70
decideForEnd(Twig_Token $token)
Definition: For.php:75
Base class for all token parsers.
Definition: TokenParser.php:18
Represents a token stream.
Definition: TokenStream.php:21
Represents a Token.
Definition: Token.php:21
const BLOCK_END_TYPE
Definition: Token.php:30
const NAME_TYPE
Definition: Token.php:32
const OPERATOR_TYPE
Definition: Token.php:35
Represents a node in the AST.
$stream
PHP stream implementation.