Extends.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Extends a template by another one.
  13. *
  14. * <pre>
  15. * {% extends "base.html" %}
  16. * </pre>
  17. */
  18. class Twig_TokenParser_Extends extends Twig_TokenParser
  19. {
  20. /**
  21. * Parses a token and returns a node.
  22. *
  23. * @param Twig_Token $token A Twig_Token instance
  24. *
  25. * @return Twig_NodeInterface A Twig_NodeInterface instance
  26. */
  27. public function parse(Twig_Token $token)
  28. {
  29. if (!$this->parser->isMainScope()) {
  30. throw new Twig_Error_Syntax('Cannot extend from a block', $token->getLine());
  31. }
  32. if (null !== $this->parser->getParent()) {
  33. throw new Twig_Error_Syntax('Multiple extends tags are forbidden', $token->getLine());
  34. }
  35. $this->parser->setParent($this->parser->getExpressionParser()->parseExpression());
  36. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  37. return null;
  38. }
  39. /**
  40. * Gets the tag name associated with this token parser.
  41. *
  42. * @return string The tag name
  43. */
  44. public function getTag()
  45. {
  46. return 'extends';
  47. }
  48. }