4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Imports blocks defined in another template into the current template.
16 * {% extends "base.html" %}
18 * {% use "blocks.html" %}
20 * {% block title %}{% endblock %}
21 * {% block content %}{% endblock %}
24 * @see https://twig.symfony.com/doc/templates.html#horizontal-reuse for details.
28 class Twig_TokenParser_Use extends Twig_TokenParser
30 public function parse(Twig_Token $token)
32 $template = $this->parser->getExpressionParser()->parseExpression();
33 $stream = $this->parser->getStream();
35 if (!$template instanceof Twig_Node_Expression_Constant) {
36 throw new Twig_Error_Syntax('The template references in a "use" statement must be a string.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
40 if ($stream->nextIf('with')) {
42 $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
45 if ($stream->nextIf('as')) {
46 $alias = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
49 $targets[$name] = new Twig_Node_Expression_Constant($alias, -1);
51 if (!$stream->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) {
57 $stream->expect(Twig_Token::BLOCK_END_TYPE);
59 $this->parser->addTrait(new Twig_Node(array('template' => $template, 'targets' => new Twig_Node($targets))));
61 return new Twig_Node();
64 public function getTag()
70 class_alias('Twig_TokenParser_Use', 'Twig\TokenParser\UseTokenParser', false);