浏览代码

[TwigBundle] fixed include tag to reflect the new syntax from Twig

Fabien Potencier 14 年之前
父节点
当前提交
84cf5698c5

+ 17 - 5
src/Symfony/Bundle/TwigBundle/Node/IncludeNode.php

@@ -18,9 +18,9 @@ namespace Symfony\Bundle\TwigBundle\Node;
  */
 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 @@ class IncludeNode extends \Twig_Node
             ->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");

+ 9 - 1
src/Symfony/Bundle/TwigBundle/TokenParser/IncludeTokenParser.php

@@ -37,9 +37,17 @@ class IncludeTokenParser extends \Twig_TokenParser
 
             $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());
     }
 
     /**