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

Open in your IDE?
  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\Variable\LocalVariable;
  12. use Twig\Node\Node;
  13. use Twig\Node\Nodes;
  14. use Twig\Node\PrintNode;
  15. use Twig\Node\SetNode;
  16. use Twig\Token;
  17. /**
  18.  * Applies filters on a section of a template.
  19.  *
  20.  *   {% apply upper %}
  21.  *      This text becomes uppercase
  22.  *   {% endapply %}
  23.  *
  24.  * @internal
  25.  */
  26. final class ApplyTokenParser extends AbstractTokenParser
  27. {
  28.     public function parse(Token $token): Node
  29.     {
  30.         $lineno $token->getLine();
  31.         $ref = new LocalVariable(null$lineno);
  32.         $filter $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref);
  33.         $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
  34.         $body $this->parser->subparse([$this'decideApplyEnd'], true);
  35.         $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
  36.         return new Nodes([
  37.             new SetNode(true$ref$body$lineno),
  38.             new PrintNode($filter$lineno),
  39.         ], $lineno);
  40.     }
  41.     public function decideApplyEnd(Token $token): bool
  42.     {
  43.         return $token->test('endapply');
  44.     }
  45.     public function getTag(): string
  46.     {
  47.         return 'apply';
  48.     }
  49. }