Include.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. * Includes a template.
  13. *
  14. * <pre>
  15. * {% include 'header.html' %}
  16. * Body
  17. * {% include 'footer.html' %}
  18. * </pre>
  19. */
  20. class Twig_TokenParser_Include extends Twig_TokenParser
  21. {
  22. /**
  23. * Parses a token and returns a node.
  24. *
  25. * @param Twig_Token $token A Twig_Token instance
  26. *
  27. * @return Twig_NodeInterface A Twig_NodeInterface instance
  28. */
  29. public function parse(Twig_Token $token)
  30. {
  31. $expr = $this->parser->getExpressionParser()->parseExpression();
  32. $variables = null;
  33. if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'with')) {
  34. $this->parser->getStream()->next();
  35. $variables = $this->parser->getExpressionParser()->parseExpression();
  36. }
  37. $only = false;
  38. if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'only')) {
  39. $this->parser->getStream()->next();
  40. $only = true;
  41. }
  42. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  43. return new Twig_Node_Include($expr, $variables, $only, $token->getLine(), $this->getTag());
  44. }
  45. /**
  46. * Gets the tag name associated with this token parser.
  47. *
  48. * @param string The tag name
  49. */
  50. public function getTag()
  51. {
  52. return 'include';
  53. }
  54. }