Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ForTokenParser.php

class ForTokenParser

Loops over each item of a sequence.

<ul> {% for user in users %} <li>{{ user.username|e }}</li> {% endfor %} </ul>

@internal

Hierarchy

  • class \Twig\TokenParser\AbstractTokenParser implements \Twig\TokenParser\TokenParserInterface
    • class \Twig\TokenParser\ForTokenParser extends \Twig\TokenParser\AbstractTokenParser

Expanded class hierarchy of ForTokenParser

1 file declares its use of ForTokenParser
CoreExtension.php in vendor/twig/twig/src/Extension/CoreExtension.php

File

vendor/twig/twig/src/TokenParser/ForTokenParser.php, line 31

Namespace

Twig\TokenParser
View source
final class ForTokenParser extends AbstractTokenParser {
    public function parse(Token $token) : Node {
        $lineno = $token->getLine();
        $stream = $this->parser
            ->getStream();
        $targets = $this->parser
            ->getExpressionParser()
            ->parseAssignmentExpression();
        $stream->expect(Token::OPERATOR_TYPE, 'in');
        $seq = $this->parser
            ->getExpressionParser()
            ->parseExpression();
        $stream->expect(Token::BLOCK_END_TYPE);
        $body = $this->parser
            ->subparse([
            $this,
            'decideForFork',
        ]);
        if ('else' == $stream->next()
            ->getValue()) {
            $stream->expect(Token::BLOCK_END_TYPE);
            $else = $this->parser
                ->subparse([
                $this,
                'decideForEnd',
            ], true);
        }
        else {
            $else = null;
        }
        $stream->expect(Token::BLOCK_END_TYPE);
        if (\count($targets) > 1) {
            $keyTarget = $targets->getNode('0');
            $keyTarget = new AssignContextVariable($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
            $valueTarget = $targets->getNode('1');
        }
        else {
            $keyTarget = new AssignContextVariable('_key', $lineno);
            $valueTarget = $targets->getNode('0');
        }
        $valueTarget = new AssignContextVariable($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
        return new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, $lineno);
    }
    public function decideForFork(Token $token) : bool {
        return $token->test([
            'else',
            'endfor',
        ]);
    }
    public function decideForEnd(Token $token) : bool {
        return $token->test('endfor');
    }
    public function getTag() : string {
        return 'for';
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractTokenParser::$parser protected property
AbstractTokenParser::setParser public function Sets the parser associated with this token parser. Overrides TokenParserInterface::setParser
ForTokenParser::decideForEnd public function
ForTokenParser::decideForFork public function
ForTokenParser::getTag public function Gets the tag name associated with this token parser. Overrides TokenParserInterface::getTag
ForTokenParser::parse public function Parses a token and returns a node. Overrides TokenParserInterface::parse
RSS feed
Powered by Drupal