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

58 lines
1.3 KiB
PHP
Raw Normal View History

2024-06-20 14:10:42 +00:00
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\TokenParser;
2025-01-13 09:56:01 +00:00
use Twig\Node\Expression\Variable\LocalVariable;
2024-06-20 14:10:42 +00:00
use Twig\Node\Node;
2025-01-13 09:56:01 +00:00
use Twig\Node\Nodes;
2024-06-20 14:10:42 +00:00
use Twig\Node\PrintNode;
use Twig\Node\SetNode;
use Twig\Token;
/**
* Applies filters on a section of a template.
*
* {% apply upper %}
* This text becomes uppercase
* {% endapply %}
*
* @internal
*/
final class ApplyTokenParser extends AbstractTokenParser
{
public function parse(Token $token): Node
{
$lineno = $token->getLine();
2025-01-13 09:56:01 +00:00
$ref = new LocalVariable(null, $lineno);
$filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref);
2024-06-20 14:10:42 +00:00
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
$body = $this->parser->subparse([$this, 'decideApplyEnd'], true);
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
2025-01-13 09:56:01 +00:00
return new Nodes([
new SetNode(true, $ref, $body, $lineno),
new PrintNode($filter, $lineno),
], $lineno);
2024-06-20 14:10:42 +00:00
}
public function decideApplyEnd(Token $token): bool
{
return $token->test('endapply');
}
public function getTag(): string
{
return 'apply';
}
}