Embed.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. public function parse(Twig_Token $token)
  16. {
  17. $stream = $this->parser->getStream();
  18. $parent = $this->parser->getExpressionParser()->parseExpression();
  19. list($variables, $only, $ignoreMissing) = $this->parseArguments();
  20. // inject a fake parent to make the parent() function work
  21. $stream->injectTokens(array(
  22. new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', $token->getLine()),
  23. new Twig_Token(Twig_Token::NAME_TYPE, 'extends', $token->getLine()),
  24. new Twig_Token(Twig_Token::STRING_TYPE, '__parent__', $token->getLine()),
  25. new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', $token->getLine()),
  26. ));
  27. $module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true);
  28. // override the parent with the correct one
  29. $module->setNode('parent', $parent);
  30. $this->parser->embedTemplate($module);
  31. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  32. return new Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
  33. }
  34. public function decideBlockEnd(Twig_Token $token)
  35. {
  36. return $token->test('endembed');
  37. }
  38. public function getTag()
  39. {
  40. return 'embed';
  41. }
  42. }