Include.php 1.9KB

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