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 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 */ 27final class ApplyTokenParser extends AbstractTokenParser 28{ 29 public function parse(Token $token) 30 { 31 $lineno = $token->getLine(); 32 $name = $this->parser->getVarName(); 33 34 $ref = new TempNameExpression($name, $lineno); 35 $ref->setAttribute('always_defined', true); 36 37 $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag()); 38 39 $this->parser->getStream()->expect(Token::BLOCK_END_TYPE); 40 $body = $this->parser->subparse([$this, 'decideApplyEnd'], true); 41 $this->parser->getStream()->expect(Token::BLOCK_END_TYPE); 42 43 return new Node([ 44 new SetNode(true, $ref, $body, $lineno, $this->getTag()), 45 new PrintNode($filter, $lineno, $this->getTag()), 46 ]); 47 } 48 49 public function decideApplyEnd(Token $token) 50 { 51 return $token->test('endapply'); 52 } 53 54 public function getTag() 55 { 56 return 'apply'; 57 } 58} 59