ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_TokenParser_For Class Reference

Loops over each item of a sequence. More...

+ Inheritance diagram for Twig_TokenParser_For:
+ Collaboration diagram for Twig_TokenParser_For:

Public Member Functions

 parse (Twig_Token $token)
 Parses a token and returns a node. More...
 
 decideForFork (Twig_Token $token)
 
 decideForEnd (Twig_Token $token)
 
 getTag ()
 Gets the tag name associated with this token parser. More...
 
- Public Member Functions inherited from Twig_TokenParser
 setParser (Twig_Parser $parser)
 Sets the parser associated with this token parser. More...
 

Protected Member Functions

 checkLoopUsageCondition (Twig_TokenStream $stream, Twig_NodeInterface $node)
 
 checkLoopUsageBody (Twig_TokenStream $stream, Twig_NodeInterface $node)
 

Additional Inherited Members

- Protected Attributes inherited from Twig_TokenParser
 $parser
 

Detailed Description

Loops over each item of a sequence.

% for user in users %} {{ user.username|e }} {% endfor %}

Definition at line 26 of file For.php.

Member Function Documentation

◆ checkLoopUsageBody()

Twig_TokenParser_For::checkLoopUsageBody ( Twig_TokenStream  $stream,
Twig_NodeInterface  $node 
)
protected

Definition at line 98 of file For.php.

References $n, array, and Twig_TokenStream\getSourceContext().

Referenced by parse().

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  }
getSourceContext()
Gets the source associated with this stream.
checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:98
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
$n
Definition: RandomTest.php:85
Create styles array
The data for the language used.
Represents a for node.
Definition: For.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkLoopUsageCondition()

Twig_TokenParser_For::checkLoopUsageCondition ( Twig_TokenStream  $stream,
Twig_NodeInterface  $node 
)
protected

Definition at line 81 of file For.php.

References $n, and Twig_TokenStream\getSourceContext().

Referenced by parse().

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  }
getSourceContext()
Gets the source associated with this stream.
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:81
$n
Definition: RandomTest.php:85
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ decideForEnd()

Twig_TokenParser_For::decideForEnd ( Twig_Token  $token)

Definition at line 75 of file For.php.

References Twig_Token\test().

76  {
77  return $token->test('endfor');
78  }
test($type, $values=null)
Tests the current token for a type and/or a value.
Definition: Token.php:70
+ Here is the call graph for this function:

◆ decideForFork()

Twig_TokenParser_For::decideForFork ( Twig_Token  $token)

Definition at line 70 of file For.php.

References array, and Twig_Token\test().

71  {
72  return $token->test(array('else', 'endfor'));
73  }
test($type, $values=null)
Tests the current token for a type and/or a value.
Definition: Token.php:70
Create styles array
The data for the language used.
+ Here is the call graph for this function:

◆ getTag()

Twig_TokenParser_For::getTag ( )

Gets the tag name associated with this token parser.

Returns
string The tag name

Implements Twig_TokenParserInterface.

Definition at line 121 of file For.php.

Referenced by parse().

122  {
123  return 'for';
124  }
+ Here is the caller graph for this function:

◆ parse()

Twig_TokenParser_For::parse ( Twig_Token  $token)

Parses a token and returns a node.

Returns
Twig_NodeInterface
Exceptions
Twig_Error_Syntax

Implements Twig_TokenParserInterface.

Definition at line 28 of file For.php.

References GuzzleHttp\Psr7\$stream, array, Twig_Token\BLOCK_END_TYPE, checkLoopUsageBody(), checkLoopUsageCondition(), Twig_Node\getAttribute(), Twig_Token\getLine(), Twig_Node\getNode(), getTag(), Twig_Node\getTemplateLine(), Twig_Token\NAME_TYPE, and Twig_Token\OPERATOR_TYPE.

29  {
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();
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  }
checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:98
$stream
PHP stream implementation.
checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:81
getAttribute($name)
Definition: Node.php:152
getTemplateLine()
Definition: Node.php:121
Create styles array
The data for the language used.
getNode($name)
Definition: Node.php:186
getTag()
Gets the tag name associated with this token parser.
Definition: For.php:121
Represents a for node.
Definition: For.php:18
getLine()
Definition: Token.php:87
const OPERATOR_TYPE
Definition: Token.php:35
const NAME_TYPE
Definition: Token.php:32
const BLOCK_END_TYPE
Definition: Token.php:30
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: