blob: 4dbf30406b0bed39f6773c4b4d96fb9b37892444 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?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\Node\Expression\TempNameExpression;
15use Twig\Node\Node;
16use Twig\Node\PrintNode;
17use Twig\Node\SetNode;
18use Twig\Token;
19
20/**
21 * Applies filters on a section of a template.
22 *
23 * {% apply upper %}
24 * This text becomes uppercase
25 * {% endapply %}
26 *
27 * @internal
28 */
29final class ApplyTokenParser extends AbstractTokenParser
30{
31 public function parse(Token $token): Node
32 {
33 $lineno = $token->getLine();
34 $name = $this->parser->getVarName();
35
36 $ref = new TempNameExpression($name, $lineno);
37 $ref->setAttribute('always_defined', true);
38
39 $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag());
40
41 $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
42 $body = $this->parser->subparse([$this, 'decideApplyEnd'], true);
43 $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
44
45 return new Node([
46 new SetNode(true, $ref, $body, $lineno, $this->getTag()),
47 new PrintNode($filter, $lineno, $this->getTag()),
48 ]);
49 }
50
51 public function decideApplyEnd(Token $token): bool
52 {
53 return $token->test('endapply');
54 }
55
56 public function getTag(): string
57 {
58 return 'apply';
59 }
60}