Embed.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2012 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Embeds a template.
  12. */
  13. class Twig_TokenParser_Embed extends Twig_TokenParser_Include
  14. {
  15. /**
  16. * Parses a token and returns a node.
  17. *
  18. * @param Twig_Token $token A Twig_Token instance
  19. *
  20. * @return Twig_NodeInterface A Twig_NodeInterface instance
  21. */
  22. public function parse(Twig_Token $token)
  23. {
  24. $stream = $this->parser->getStream();
  25. $parent = $this->parser->getExpressionParser()->parseExpression();
  26. list($variables, $only, $ignoreMissing) = $this->parseArguments();
  27. // inject a fake parent to make the parent() function work
  28. $stream->injectTokens(array(
  29. new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', $token->getLine()),
  30. new Twig_Token(Twig_Token::NAME_TYPE, 'extends', $token->getLine()),
  31. new Twig_Token(Twig_Token::STRING_TYPE, '__parent__', $token->getLine()),
  32. new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', $token->getLine()),
  33. ));
  34. $module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true);
  35. // override the parent with the correct one
  36. $module->setNode('parent', $parent);
  37. $this->parser->embedTemplate($module);
  38. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  39. return new Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
  40. }
  41. public function decideBlockEnd(Twig_Token $token)
  42. {
  43. return $token->test('endembed');
  44. }
  45. /**
  46. * Gets the tag name associated with this token parser.
  47. *
  48. * @return string The tag name
  49. */
  50. public function getTag()
  51. {
  52. return 'embed';
  53. }
  54. }