| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Twig. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier |
| 7 |
* (c) Armin Ronacher |
| 8 |
* |
| 9 |
* For the full copyright and license information, please view the LICENSE |
| 10 |
* file that was distributed with this source code. |
| 11 |
*/ |
| 12 |
namespace ElementorDeps\Twig\TokenParser; |
| 13 |
|
| 14 |
use ElementorDeps\Twig\Error\SyntaxError; |
| 15 |
use ElementorDeps\Twig\Node\BlockNode; |
| 16 |
use ElementorDeps\Twig\Node\BlockReferenceNode; |
| 17 |
use ElementorDeps\Twig\Node\Node; |
| 18 |
use ElementorDeps\Twig\Node\PrintNode; |
| 19 |
use ElementorDeps\Twig\Token; |
| 20 |
/** |
| 21 |
* Marks a section of a template as being reusable. |
| 22 |
* |
| 23 |
* {% block head %} |
| 24 |
* <link rel="stylesheet" href="style.css" /> |
| 25 |
* <title>{% block title %}{% endblock %} - My Webpage</title> |
| 26 |
* {% endblock %} |
| 27 |
* |
| 28 |
* @internal |
| 29 |
*/ |
| 30 |
final class BlockTokenParser extends AbstractTokenParser |
| 31 |
{ |
| 32 |
public function parse(Token $token) : Node |
| 33 |
{ |
| 34 |
$lineno = $token->getLine(); |
| 35 |
$stream = $this->parser->getStream(); |
| 36 |
$name = $stream->expect( |
| 37 |
/* Token::NAME_TYPE */ |
| 38 |
5 |
| 39 |
)->getValue(); |
| 40 |
if ($this->parser->hasBlock($name)) { |
| 41 |
throw new SyntaxError(\sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
| 42 |
} |
| 43 |
$this->parser->setBlock($name, $block = new BlockNode($name, new Node([]), $lineno)); |
| 44 |
$this->parser->pushLocalScope(); |
| 45 |
$this->parser->pushBlockStack($name); |
| 46 |
if ($stream->nextIf( |
| 47 |
/* Token::BLOCK_END_TYPE */ |
| 48 |
3 |
| 49 |
)) { |
| 50 |
$body = $this->parser->subparse([$this, 'decideBlockEnd'], \true); |
| 51 |
if ($token = $stream->nextIf( |
| 52 |
/* Token::NAME_TYPE */ |
| 53 |
5 |
| 54 |
)) { |
| 55 |
$value = $token->getValue(); |
| 56 |
if ($value != $name) { |
| 57 |
throw new SyntaxError(\sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getSourceContext()); |
| 58 |
} |
| 59 |
} |
| 60 |
} else { |
| 61 |
$body = new Node([new PrintNode($this->parser->getExpressionParser()->parseExpression(), $lineno)]); |
| 62 |
} |
| 63 |
$stream->expect( |
| 64 |
/* Token::BLOCK_END_TYPE */ |
| 65 |
3 |
| 66 |
); |
| 67 |
$block->setNode('body', $body); |
| 68 |
$this->parser->popBlockStack(); |
| 69 |
$this->parser->popLocalScope(); |
| 70 |
return new BlockReferenceNode($name, $lineno, $this->getTag()); |
| 71 |
} |
| 72 |
public function decideBlockEnd(Token $token) : bool |
| 73 |
{ |
| 74 |
return $token->test('endblock'); |
| 75 |
} |
| 76 |
public function getTag() : string |
| 77 |
{ |
| 78 |
return 'block'; |
| 79 |
} |
| 80 |
} |
| 81 |
|