Explorar el Código

[TwigBundle] fixed the include tag to behave like the standard Twig include tag

Fabien Potencier hace 14 años
padre
commit
dd7e33af6b

+ 11 - 3
src/Symfony/Bundle/TwigBundle/Extension/HelpersExtension.php

@@ -4,6 +4,7 @@ namespace Symfony\Bundle\TwigBundle\Extension;
 
 use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
 use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Bundle\TwigBundle\TokenParser\IncludeTokenParser;
 
 /*
  * This file is part of the Symfony package.
@@ -18,13 +19,15 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  *
  * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class HelpersExtension extends \Twig_Extension
+class TemplatingExtension extends \Twig_Extension
 {
     protected $container;
+    protected $templating;
 
     public function __construct(ContainerInterface $container)
     {
         $this->container = $container;
+        $this->templating = $container->get('templating.engine');
     }
 
     public function getContainer()
@@ -32,6 +35,11 @@ class HelpersExtension extends \Twig_Extension
         return $this->container;
     }
 
+    public function getTemplating()
+    {
+        return $this->templating;
+    }
+
     /**
      * Returns the token parser instance to add to the existing list.
      *
@@ -65,7 +73,7 @@ class HelpersExtension extends \Twig_Extension
             new HelperTokenParser('flash', '<name>', 'templating.helper.session', 'getFlash'),
 
             // {% include 'sometemplate.php' with ['something' : 'something2'] %}
-            new HelperTokenParser('include', '<name> [with <arguments:array>]', 'templating.engine', 'render'),
+            new IncludeTokenParser(),
         );
     }
 
@@ -76,6 +84,6 @@ class HelpersExtension extends \Twig_Extension
      */
     public function getName()
     {
-        return 'symfony.helpers';
+        return 'templating';
     }
 }

+ 1 - 1
src/Symfony/Bundle/TwigBundle/Node/HelperNode.php

@@ -33,7 +33,7 @@ class HelperNode extends \Twig_Node
         $compiler
             ->addDebugInfo($this)
             ->raw("\$this->env->getExtension(")
-            ->string('symfony.helpers')
+            ->string('templating')
             ->raw(")->getContainer()->get(")
             ->string($this->getAttribute('helper'))
             ->raw(")->")

+ 48 - 0
src/Symfony/Bundle/TwigBundle/Node/IncludeNode.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace Symfony\Bundle\TwigBundle\Node;
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Represents an include node.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class IncludeNode extends \Twig_Node
+{
+    public function __construct(\Twig_Node_Expression $expr, \Twig_Node_Expression $variables = null, $lineno, $tag = null)
+    {
+        parent::__construct(array('expr' => $expr, 'variables' => $variables), array(), $lineno, $tag);
+    }
+
+    /**
+     * Compiles the node to PHP.
+     *
+     * @param \Twig_Compiler A Twig_Compiler instance
+     */
+    public function compile($compiler)
+    {
+        $compiler
+            ->addDebugInfo($this)
+            ->write('echo $this->env->getExtension(\'templating\')->getTemplating()->render(')
+            ->subcompile($this->getNode('expr'))
+            ->raw(', ')
+        ;
+
+        if (null === $this->getNode('variables')) {
+            $compiler->raw('$context');
+        } else {
+            $compiler->subcompile($this->getNode('variables'));
+        }
+
+        $compiler->raw(");\n");
+    }
+}

+ 1 - 1
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -48,7 +48,7 @@
             <argument type="service" id="templating.html.generator" />
         </service>
 
-        <service id="twig.extension.helpers" class="Symfony\Bundle\TwigBundle\Extension\HelpersExtension">
+        <service id="twig.extension.helpers" class="Symfony\Bundle\TwigBundle\Extension\TemplatingExtension">
             <tag name="twig.extension" />
             <argument type="service" id="service_container" />
         </service>

+ 54 - 0
src/Symfony/Bundle/TwigBundle/TokenParser/IncludeTokenParser.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace Symfony\Bundle\TwigBundle\TokenParser;
+
+use Symfony\Bundle\TwigBundle\Node\IncludeNode;
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * 
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class IncludeTokenParser extends \Twig_TokenParser
+{
+    /**
+     * Parses a token and returns a node.
+     *
+     * @param \Twig_Token $token A \Twig_Token instance
+     *
+     * @return \Twig_NodeInterface A \Twig_NodeInterface instance
+     */
+    public function parse(\Twig_Token $token)
+    {
+        $expr = $this->parser->getExpressionParser()->parseExpression();
+
+        $variables = null;
+        if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) {
+            $this->parser->getStream()->next();
+
+            $variables = $this->parser->getExpressionParser()->parseExpression();
+        }
+        $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
+
+        return new IncludeNode($expr, $variables, $token->getLine(), $this->getTag());
+    }
+
+    /**
+     * Gets the tag name associated with this token parser.
+     *
+     * @param string The tag name
+     */
+    public function getTag()
+    {
+        return 'include';
+    }
+}