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

Breadcrumb

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

function IfTokenParser::parse

Overrides TokenParserInterface::parse

File

vendor/twig/twig/src/TokenParser/IfTokenParser.php, line 36

Class

IfTokenParser
Tests a condition.

Namespace

Twig\TokenParser

Code

public function parse(Token $token) : Node {
    $lineno = $token->getLine();
    $expr = $this->parser
        ->getExpressionParser()
        ->parseExpression();
    $stream = $this->parser
        ->getStream();
    $stream->expect(Token::BLOCK_END_TYPE);
    $body = $this->parser
        ->subparse([
        $this,
        'decideIfFork',
    ]);
    $tests = [
        $expr,
        $body,
    ];
    $else = null;
    $end = false;
    while (!$end) {
        switch ($stream->next()
            ->getValue()) {
            case 'else':
                $stream->expect(Token::BLOCK_END_TYPE);
                $else = $this->parser
                    ->subparse([
                    $this,
                    'decideIfEnd',
                ]);
                break;
            case 'elseif':
                $expr = $this->parser
                    ->getExpressionParser()
                    ->parseExpression();
                $stream->expect(Token::BLOCK_END_TYPE);
                $body = $this->parser
                    ->subparse([
                    $this,
                    'decideIfFork',
                ]);
                $tests[] = $expr;
                $tests[] = $body;
                break;
            case 'endif':
                $end = true;
                break;
            default:
                throw new SyntaxError(\sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d).', $lineno), $stream->getCurrent()
                    ->getLine(), $stream->getSourceContext());
        }
    }
    $stream->expect(Token::BLOCK_END_TYPE);
    return new IfNode(new Nodes($tests), $else, $lineno);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal