Include.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. $ignoreMissing = false;
  33. if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'ignore')) {
  34. $this->parser->getStream()->next();
  35. $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, 'missing');
  36. $ignoreMissing = true;
  37. }
  38. $variables = null;
  39. if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'with')) {
  40. $this->parser->getStream()->next();
  41. $variables = $this->parser->getExpressionParser()->parseExpression();
  42. }
  43. $only = false;
  44. if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'only')) {
  45. $this->parser->getStream()->next();
  46. $only = true;
  47. }
  48. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  49. return new Twig_Node_Include($expr, $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
  50. }
  51. /**
  52. * Gets the tag name associated with this token parser.
  53. *
  54. * @return string The tag name
  55. */
  56. public function getTag()
  57. {
  58. return 'include';
  59. }
  60. }