Embed.php (1693B)
1 <?php 2 3 /* 4 * This file is part of Twig. 5 * 6 * (c) 2012 Fabien Potencier 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 /** 13 * Embeds a template. 14 */ 15 class Twig_TokenParser_Embed extends Twig_TokenParser_Include 16 { 17 public function parse(Twig_Token $token) 18 { 19 $stream = $this->parser->getStream(); 20 21 $parent = $this->parser->getExpressionParser()->parseExpression(); 22 23 list($variables, $only, $ignoreMissing) = $this->parseArguments(); 24 25 // inject a fake parent to make the parent() function work 26 $stream->injectTokens(array( 27 new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', $token->getLine()), 28 new Twig_Token(Twig_Token::NAME_TYPE, 'extends', $token->getLine()), 29 new Twig_Token(Twig_Token::STRING_TYPE, '__parent__', $token->getLine()), 30 new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', $token->getLine()), 31 )); 32 33 $module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true); 34 35 // override the parent with the correct one 36 $module->setNode('parent', $parent); 37 38 $this->parser->embedTemplate($module); 39 40 $stream->expect(Twig_Token::BLOCK_END_TYPE); 41 42 return new Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag()); 43 } 44 45 public function decideBlockEnd(Twig_Token $token) 46 { 47 return $token->test('endembed'); 48 } 49 50 public function getTag() 51 { 52 return 'embed'; 53 } 54 }