ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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...
 
 setParser (Twig_Parser $parser)
 Sets the parser associated with this token parser. More...
 
 parse (Twig_Token $token)
 Parses a token and returns a node. More...
 
 getTag ()
 Gets the tag name 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 %}

@final

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.

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 }
$n
Definition: RandomTest.php:85
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
checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:98
$stream
PHP stream implementation.

References $n, GuzzleHttp\Psr7\$stream, and checkLoopUsageBody().

Referenced by checkLoopUsageBody(), and parse().

+ 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.

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 }
checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
Definition: For.php:81

References $n, GuzzleHttp\Psr7\$stream, and checkLoopUsageCondition().

Referenced by checkLoopUsageCondition(), and parse().

+ 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.

76 {
77 return $token->test('endfor');
78 }

References PHPMailer\PHPMailer\$token.

◆ decideForFork()

Twig_TokenParser_For::decideForFork ( Twig_Token  $token)

Definition at line 70 of file For.php.

71 {
72 return $token->test(array('else', 'endfor'));
73 }

References PHPMailer\PHPMailer\$token.

◆ 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.

122 {
123 return 'for';
124 }

Referenced by parse().

+ 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.

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 }
getTag()
Gets the tag name associated with this token parser.
Definition: For.php:121
const BLOCK_END_TYPE
Definition: Token.php:30
const NAME_TYPE
Definition: Token.php:32
const OPERATOR_TYPE
Definition: Token.php:35

References GuzzleHttp\Psr7\$stream, PHPMailer\PHPMailer\$token, Twig_Token\BLOCK_END_TYPE, checkLoopUsageBody(), checkLoopUsageCondition(), getTag(), Twig_Token\NAME_TYPE, and Twig_Token\OPERATOR_TYPE.

+ Here is the call graph for this function:

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