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

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