From 84cf5698c53e0eb5247179c6a541366b6ccab515 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 19 Nov 2010 09:51:12 +0100 Subject: [PATCH] [TwigBundle] fixed include tag to reflect the new syntax from Twig --- .../Bundle/TwigBundle/Node/IncludeNode.php | 22 ++++++++++++++----- .../TokenParser/IncludeTokenParser.php | 10 ++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Node/IncludeNode.php b/src/Symfony/Bundle/TwigBundle/Node/IncludeNode.php index 06c21abee2a3..fd2247030cd4 100644 --- a/src/Symfony/Bundle/TwigBundle/Node/IncludeNode.php +++ b/src/Symfony/Bundle/TwigBundle/Node/IncludeNode.php @@ -18,9 +18,9 @@ */ class IncludeNode extends \Twig_Node { - public function __construct(\Twig_Node_Expression $expr, \Twig_Node_Expression $variables = null, $lineno, $tag = null) + public function __construct(\Twig_Node_Expression $expr, \Twig_Node_Expression $variables = null, $only = false, $lineno, $tag = null) { - parent::__construct(array('expr' => $expr, 'variables' => $variables), array(), $lineno, $tag); + parent::__construct(array('expr' => $expr, 'variables' => $variables), array('only' => (Boolean) $only), $lineno, $tag); } /** @@ -37,10 +37,22 @@ public function compile($compiler) ->raw(', ') ; - if (null === $this->getNode('variables')) { - $compiler->raw('$context'); + if (false === $this->getAttribute('only')) { + if (null === $this->getNode('variables')) { + $compiler->raw('$context'); + } else { + $compiler + ->raw('array_merge($context, ') + ->subcompile($this->getNode('variables')) + ->raw(')') + ; + } } else { - $compiler->subcompile($this->getNode('variables')); + if (null === $this->getNode('variables')) { + $compiler->raw('array()'); + } else { + $compiler->subcompile($this->getNode('variables')); + } } $compiler->raw(");\n"); diff --git a/src/Symfony/Bundle/TwigBundle/TokenParser/IncludeTokenParser.php b/src/Symfony/Bundle/TwigBundle/TokenParser/IncludeTokenParser.php index b3654a2c16db..28bdd007280b 100644 --- a/src/Symfony/Bundle/TwigBundle/TokenParser/IncludeTokenParser.php +++ b/src/Symfony/Bundle/TwigBundle/TokenParser/IncludeTokenParser.php @@ -37,9 +37,17 @@ public function parse(\Twig_Token $token) $variables = $this->parser->getExpressionParser()->parseExpression(); } + + $only = false; + if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'only')) { + $this->parser->getStream()->next(); + + $only = true; + } + $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - return new IncludeNode($expr, $variables, $token->getLine(), $this->getTag()); + return new IncludeNode($expr, $variables, $only, $token->getLine(), $this->getTag()); } /**