Browse Source

[TwigBundle] added request and session as global variables

 * removed the "_view" variable from templates
 * removed the "flash()" function (now available from the session directly {{ session.flash('notice') }})
Fabien Potencier 14 years ago
parent
commit
b60d254be2

+ 5 - 0
src/Symfony/Bundle/FrameworkBundle/Templating/Engine.php

@@ -52,6 +52,11 @@ class Engine extends BaseEngine
         }
     }
 
+    public function getContainer()
+    {
+        return $this->container;
+    }
+
     /**
      * Renders a view and returns a Response.
      *

+ 0 - 6
src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php

@@ -74,7 +74,6 @@ class TemplatingExtension extends \Twig_Extension
             'url'   => new \Twig_Function_Method($this, 'getUrl'),
             'path'  => new \Twig_Function_Method($this, 'getPath'),
             'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
-            'flash' => new \Twig_Function_Method($this, 'getFlash'),
         );
     }
 
@@ -93,11 +92,6 @@ class TemplatingExtension extends \Twig_Extension
         return $this->container->get('templating.helper.assets')->getUrl($location);
     }
 
-    public function getFlash($name, $default = null)
-    {
-        return $this->container->get('request')->getSession()->getFlash($name, $default);
-    }
-
     /**
      * Returns the token parser instance to add to the existing list.
      *

+ 4 - 1
src/Symfony/Bundle/TwigBundle/Renderer/Renderer.php

@@ -37,7 +37,10 @@ class Renderer extends BaseRenderer
      */
     public function evaluate(Storage $template, array $parameters = array())
     {
-        $parameters['_view'] = $this->engine;
+        // cannot be set in the constructor as we need the current request
+        $request = $this->engine->getContainer()->get('request');
+        $this->environment->addGlobal('request', $request);
+        $this->environment->addGlobal('session', $request->getSession());
 
         return $this->environment->loadTemplate($template)->render($parameters);
     }