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

Breadcrumb

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

class FromTokenParser

Imports macros.

{% from 'forms.html' import forms %}

@internal

Hierarchy

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

Expanded class hierarchy of FromTokenParser

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

File

vendor/twig/twig/src/TokenParser/FromTokenParser.php, line 28

Namespace

Twig\TokenParser
View source
final class FromTokenParser extends AbstractTokenParser {
    public function parse(Token $token) : Node {
        $macro = $this->parser
            ->getExpressionParser()
            ->parseExpression();
        $stream = $this->parser
            ->getStream();
        $stream->expect(Token::NAME_TYPE, 'import');
        $targets = [];
        while (true) {
            $name = $stream->expect(Token::NAME_TYPE)
                ->getValue();
            if ($stream->nextIf('as')) {
                $alias = new AssignContextVariable($stream->expect(Token::NAME_TYPE)
                    ->getValue(), $token->getLine());
            }
            else {
                $alias = new AssignContextVariable($name, $token->getLine());
            }
            $targets[$name] = $alias;
            if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
                break;
            }
        }
        $stream->expect(Token::BLOCK_END_TYPE);
        $internalRef = new AssignTemplateVariable(new TemplateVariable(null, $token->getLine()), $this->parser
            ->isMainScope());
        $node = new ImportNode($macro, $internalRef, $token->getLine());
        foreach ($targets as $name => $alias) {
            $this->parser
                ->addImportedSymbol('function', $alias->getAttribute('name'), 'macro_' . $name, $internalRef);
        }
        return $node;
    }
    public function getTag() : string {
        return 'from';
    }

}

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