Filter.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 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. /**
  11. * Filters a section of a template by applying filters.
  12. *
  13. * <pre>
  14. * {% filter upper %}
  15. * This text becomes uppercase
  16. * {% endfilter %}
  17. * </pre>
  18. */
  19. class Twig_TokenParser_Filter extends Twig_TokenParser
  20. {
  21. public function parse(Twig_Token $token)
  22. {
  23. $name = $this->parser->getVarName();
  24. $ref = new Twig_Node_Expression_BlockReference(new Twig_Node_Expression_Constant($name, $token->getLine()), true, $token->getLine(), $this->getTag());
  25. $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag());
  26. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  27. $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
  28. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  29. $block = new Twig_Node_Block($name, $body, $token->getLine());
  30. $this->parser->setBlock($name, $block);
  31. return new Twig_Node_Print($filter, $token->getLine(), $this->getTag());
  32. }
  33. public function decideBlockEnd(Twig_Token $token)
  34. {
  35. return $token->test('endfilter');
  36. }
  37. public function getTag()
  38. {
  39. return 'filter';
  40. }
  41. }