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

Breadcrumb

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

class UseTokenParser

Imports blocks defined in another template into the current template.

{% extends "base.html" %}

{% use "blocks.html" %}

{% block title %}{% endblock %} {% block content %}{% endblock %}

@internal

Hierarchy

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

Expanded class hierarchy of UseTokenParser

See also

https://twig.symfony.com/doc/templates.html#horizontal-reuse for details.

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

File

vendor/twig/twig/src/TokenParser/UseTokenParser.php, line 35

Namespace

Twig\TokenParser
View source
final class UseTokenParser extends AbstractTokenParser {
    public function parse(Token $token) : Node {
        $template = $this->parser
            ->getExpressionParser()
            ->parseExpression();
        $stream = $this->parser
            ->getStream();
        if (!$template instanceof ConstantExpression) {
            throw new SyntaxError('The template references in a "use" statement must be a string.', $stream->getCurrent()
                ->getLine(), $stream->getSourceContext());
        }
        $targets = [];
        if ($stream->nextIf('with')) {
            while (true) {
                $name = $stream->expect(Token::NAME_TYPE)
                    ->getValue();
                $alias = $name;
                if ($stream->nextIf('as')) {
                    $alias = $stream->expect(Token::NAME_TYPE)
                        ->getValue();
                }
                $targets[$name] = new ConstantExpression($alias, -1);
                if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
                    break;
                }
            }
        }
        $stream->expect(Token::BLOCK_END_TYPE);
        $this->parser
            ->addTrait(new Nodes([
            'template' => $template,
            'targets' => new Nodes($targets),
        ]));
        return new EmptyNode($token->getLine());
    }
    public function getTag() : string {
        return 'use';
    }

}

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