Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 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 | |
| 12 | namespace Twig\TokenParser; |
| 13 | |
| 14 | use Twig\Node\Expression\TempNameExpression; |
| 15 | use Twig\Node\Node; |
| 16 | use Twig\Node\PrintNode; |
| 17 | use Twig\Node\SetNode; |
| 18 | use 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 | */ |
| 29 | final 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 | } |