1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig\TokenParser; 13 14use Twig\Error\SyntaxError; 15use Twig\Node\Expression\ConstantExpression; 16use Twig\Node\Node; 17use Twig\Token; 18 19/** 20 * Imports blocks defined in another template into the current template. 21 * 22 * {% extends "base.html" %} 23 * 24 * {% use "blocks.html" %} 25 * 26 * {% block title %}{% endblock %} 27 * {% block content %}{% endblock %} 28 * 29 * @see https://twig.symfony.com/doc/templates.html#horizontal-reuse for details. 30 */ 31final class UseTokenParser extends AbstractTokenParser 32{ 33 public function parse(Token $token) 34 { 35 $template = $this->parser->getExpressionParser()->parseExpression(); 36 $stream = $this->parser->getStream(); 37 38 if (!$template instanceof ConstantExpression) { 39 throw new SyntaxError('The template references in a "use" statement must be a string.', $stream->getCurrent()->getLine(), $stream->getSourceContext()); 40 } 41 42 $targets = []; 43 if ($stream->nextIf('with')) { 44 do { 45 $name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); 46 47 $alias = $name; 48 if ($stream->nextIf('as')) { 49 $alias = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue(); 50 } 51 52 $targets[$name] = new ConstantExpression($alias, -1); 53 54 if (!$stream->nextIf(/* Token::PUNCTUATION_TYPE */ 9, ',')) { 55 break; 56 } 57 } while (true); 58 } 59 60 $stream->expect(/* Token::BLOCK_END_TYPE */ 3); 61 62 $this->parser->addTrait(new Node(['template' => $template, 'targets' => new Node($targets)])); 63 64 return new Node(); 65 } 66 67 public function getTag() 68 { 69 return 'use'; 70 } 71} 72 73class_alias('Twig\TokenParser\UseTokenParser', 'Twig_TokenParser_Use'); 74