vendor/twig/twig/src/TokenParser/ApplyTokenParser.php line 31

  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Twig\TokenParser;
  11. use Twig\Node\Expression\TempNameExpression;
  12. use Twig\Node\Node;
  13. use Twig\Node\PrintNode;
  14. use Twig\Node\SetNode;
  15. use Twig\Token;
  16. /**
  17.  * Applies filters on a section of a template.
  18.  *
  19.  *   {% apply upper %}
  20.  *      This text becomes uppercase
  21.  *   {% endapply %}
  22.  *
  23.  * @internal
  24.  */
  25. final class ApplyTokenParser extends AbstractTokenParser
  26. {
  27.     public function parse(Token $token): Node
  28.     {
  29.         $lineno $token->getLine();
  30.         $name $this->parser->getVarName();
  31.         $ref = new TempNameExpression($name$lineno);
  32.         $ref->setAttribute('always_defined'true);
  33.         $filter $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref);
  34.         $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
  35.         $body $this->parser->subparse([$this'decideApplyEnd'], true);
  36.         $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
  37.         return new Node([
  38.             new SetNode(true$ref$body$lineno),
  39.             new PrintNode($filter$lineno),
  40.         ], [], $lineno);
  41.     }
  42.     public function decideApplyEnd(Token $token): bool
  43.     {
  44.         return $token->test('endapply');
  45.     }
  46.     public function getTag(): string
  47.     {
  48.         return 'apply';
  49.     }
  50. }