4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
17 class Twig_TokenParser_Embed
extends Twig_TokenParser_Include
19 public function parse(Twig_Token
$token)
21 $stream = $this->parser
->getStream();
23 $parent = $this->parser
->getExpressionParser()->parseExpression();
25 list($variables, $only, $ignoreMissing) = $this->parseArguments();
27 $parentToken = $fakeParentToken = new Twig_Token(Twig_Token
::STRING_TYPE
, '__parent__', $token->getLine());
28 if ($parent instanceof Twig_Node_Expression_Constant
) {
29 $parentToken = new Twig_Token(Twig_Token
::STRING_TYPE
, $parent->getAttribute('value'), $token->getLine());
30 } elseif ($parent instanceof Twig_Node_Expression_Name
) {
31 $parentToken = new Twig_Token(Twig_Token
::NAME_TYPE
, $parent->getAttribute('name'), $token->getLine());
34 // inject a fake parent to make the parent() function work
35 $stream->injectTokens(array(
36 new Twig_Token(Twig_Token
::BLOCK_START_TYPE
, '', $token->getLine()),
37 new Twig_Token(Twig_Token
::NAME_TYPE
, 'extends', $token->getLine()),
39 new Twig_Token(Twig_Token
::BLOCK_END_TYPE
, '', $token->getLine()),
42 $module = $this->parser
->parse($stream, array($this, 'decideBlockEnd'), true);
44 // override the parent with the correct one
45 if ($fakeParentToken === $parentToken) {
46 $module->setNode('parent', $parent);
49 $this->parser
->embedTemplate($module);
51 $stream->expect(Twig_Token
::BLOCK_END_TYPE
);
53 return new Twig_Node_Embed($module->getTemplateName(), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag());
56 public function decideBlockEnd(Twig_Token
$token)
58 return $token->test('endembed');
61 public function getTag()
67 class_alias('Twig_TokenParser_Embed', 'Twig\TokenParser\EmbedTokenParser', false);