leilukin-tumbleblog/includes/lib/Twig/TokenParser/IncludeTokenParser.php

70 lines
1.5 KiB
PHP
Raw Normal View History

2024-06-20 14:10:42 +00:00
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
* (c) Armin Ronacher
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\TokenParser;
use Twig\Node\IncludeNode;
use Twig\Node\Node;
use Twig\Token;
/**
* Includes a template.
*
2025-01-13 09:56:01 +00:00
* {% include 'header.html.twig' %}
2024-06-20 14:10:42 +00:00
* Body
2025-01-13 09:56:01 +00:00
* {% include 'footer.html.twig' %}
2024-06-20 14:10:42 +00:00
*
* @internal
*/
class IncludeTokenParser extends AbstractTokenParser
{
public function parse(Token $token): Node
{
$expr = $this->parser->getExpressionParser()->parseExpression();
[$variables, $only, $ignoreMissing] = $this->parseArguments();
2025-01-13 09:56:01 +00:00
return new IncludeNode($expr, $variables, $only, $ignoreMissing, $token->getLine());
2024-06-20 14:10:42 +00:00
}
protected function parseArguments()
{
$stream = $this->parser->getStream();
$ignoreMissing = false;
2025-01-13 09:56:01 +00:00
if ($stream->nextIf(Token::NAME_TYPE, 'ignore')) {
$stream->expect(Token::NAME_TYPE, 'missing');
2024-06-20 14:10:42 +00:00
$ignoreMissing = true;
}
$variables = null;
2025-01-13 09:56:01 +00:00
if ($stream->nextIf(Token::NAME_TYPE, 'with')) {
2024-06-20 14:10:42 +00:00
$variables = $this->parser->getExpressionParser()->parseExpression();
}
$only = false;
2025-01-13 09:56:01 +00:00
if ($stream->nextIf(Token::NAME_TYPE, 'only')) {
2024-06-20 14:10:42 +00:00
$only = true;
}
2025-01-13 09:56:01 +00:00
$stream->expect(Token::BLOCK_END_TYPE);
2024-06-20 14:10:42 +00:00
return [$variables, $only, $ignoreMissing];
}
public function getTag(): string
{
return 'include';
}
}