Spaceless.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. public function parse(Twig_Token $token)
  26. {
  27. $lineno = $token->getLine();
  28. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  29. $body = $this->parser->subparse(array($this, 'decideSpacelessEnd'), true);
  30. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  31. return new Twig_Node_Spaceless($body, $lineno, $this->getTag());
  32. }
  33. public function decideSpacelessEnd(Twig_Token $token)
  34. {
  35. return $token->test('endspaceless');
  36. }
  37. public function getTag()
  38. {
  39. return 'spaceless';
  40. }
  41. }