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