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

Breadcrumb

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

class IfTokenParser

Tests a condition.

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

@internal

Hierarchy

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

Expanded class hierarchy of IfTokenParser

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

File

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

Namespace

Twig\TokenParser
View source
final class IfTokenParser extends AbstractTokenParser {
    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);
    }
    public function decideIfFork(Token $token) : bool {
        return $token->test([
            'elseif',
            'else',
            'endif',
        ]);
    }
    public function decideIfEnd(Token $token) : bool {
        return $token->test([
            'endif',
        ]);
    }
    public function getTag() : string {
        return 'if';
    }

}

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
IfTokenParser::decideIfEnd public function
IfTokenParser::decideIfFork public function
IfTokenParser::getTag public function Gets the tag name associated with this token parser. Overrides TokenParserInterface::getTag
IfTokenParser::parse public function Parses a token and returns a node. Overrides TokenParserInterface::parse
RSS feed
Powered by Drupal