Spaceless.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 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. * Remove whitespaces between HTML tags.
  12. *
  13. * <pre>
  14. * {% spaceless %}
  15. * <div>
  16. * <strong>foo</strong>
  17. * </div>
  18. * {% endspaceless %}
  19. *
  20. * {# output will be <div><strong>foo</strong></div> #}
  21. * </pre>
  22. */
  23. class Twig_TokenParser_Spaceless extends Twig_TokenParser
  24. {
  25. /**
  26. * Parses a token and returns a node.
  27. *
  28. * @param Twig_Token $token A Twig_Token instance
  29. *
  30. * @return Twig_NodeInterface A Twig_NodeInterface instance
  31. */
  32. public function parse(Twig_Token $token)
  33. {
  34. $lineno = $token->getLine();
  35. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  36. $body = $this->parser->subparse(array($this, 'decideSpacelessEnd'), true);
  37. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  38. return new Twig_Node_Spaceless($body, $lineno, $this->getTag());
  39. }
  40. public function decideSpacelessEnd(Twig_Token $token)
  41. {
  42. return $token->test('endspaceless');
  43. }
  44. /**
  45. * Gets the tag name associated with this token parser.
  46. *
  47. * @param string The tag name
  48. */
  49. public function getTag()
  50. {
  51. return 'spaceless';
  52. }
  53. }