Include.php 1.5 KB

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. public function parse(Twig_Token $token)
  23. {
  24. $expr = $this->parser->getExpressionParser()->parseExpression();
  25. list($variables, $only, $ignoreMissing) = $this->parseArguments();
  26. return new Twig_Node_Include($expr, $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
  27. }
  28. protected function parseArguments()
  29. {
  30. $stream = $this->parser->getStream();
  31. $ignoreMissing = false;
  32. if ($stream->nextIf(Twig_Token::NAME_TYPE, 'ignore')) {
  33. $stream->expect(Twig_Token::NAME_TYPE, 'missing');
  34. $ignoreMissing = true;
  35. }
  36. $variables = null;
  37. if ($stream->nextIf(Twig_Token::NAME_TYPE, 'with')) {
  38. $variables = $this->parser->getExpressionParser()->parseExpression();
  39. }
  40. $only = false;
  41. if ($stream->nextIf(Twig_Token::NAME_TYPE, 'only')) {
  42. $only = true;
  43. }
  44. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  45. return array($variables, $only, $ignoreMissing);
  46. }
  47. public function getTag()
  48. {
  49. return 'include';
  50. }
  51. }