Bläddra i källkod

[Templating] Added Global variables as they are implemented with Twig. With tests

Henrik Bjørnskov 14 år sedan
förälder
incheckning
afc2f96549

+ 34 - 0
src/Symfony/Component/Templating/Engine.php

@@ -33,6 +33,7 @@ class Engine implements \ArrayAccess
     protected $charset;
     protected $cache;
     protected $escapers;
+    protected $globals;
 
     /**
      * Constructor.
@@ -118,6 +119,9 @@ class Engine implements \ArrayAccess
         $this->current = $name;
         $this->parents[$name] = null;
 
+        // Attach the global variables
+        $parameters = array_replace($this->getGlobals(), $parameters);
+
         // render
         if (false === $content = $this->renderers[$renderer]->evaluate($template, $parameters)) {
             throw new \RuntimeException(sprintf('The template "%s" cannot be rendered (renderer: %s).', $name, $renderer));
@@ -397,6 +401,36 @@ class Engine implements \ArrayAccess
         return $this->escapers[$context];
     }
 
+    /**
+     * @param string $name
+     * @param mixed $value
+     */
+    public function addGlobal($name, $value)
+    {
+        if (null === $this->globals) {
+            $this->getGlobals();
+        }
+
+        $this->globals[$name] = $value;
+    }
+
+    /**
+     * Returns the assigned globals. If no globals are set yet it calls all assigned helpers for theirs.
+     *
+     * @return array
+     */
+    public function getGlobals()
+    {
+        if (null === $this->globals) {
+            $this->globals = array();
+            foreach ($this->helpers as $helper) {
+                $this->globals = array_replace($this->globals, $helper->getGlobals());
+            }
+        }
+
+        return $this->globals;
+    }
+
     /**
      * Initializes the built-in escapers.
      *

+ 10 - 0
src/Symfony/Component/Templating/Helper/Helper.php

@@ -42,4 +42,14 @@ abstract class Helper implements HelperInterface
     {
         return $this->charset;
     }
+
+    /**
+     * Get global variables for templates
+     *
+     * @return array
+     */
+    public function getGlobals()
+    {
+        return array();
+    }
 }

+ 8 - 0
src/Symfony/Component/Templating/Helper/HelperInterface.php

@@ -38,4 +38,12 @@ interface HelperInterface
      * @return string The default charset
      */
     function getCharset();
+
+    /**
+     * Returns a key value array with variables that should be accessible
+     * in templates.
+     *
+     * @return array
+     */
+    function getGlobals();
 }

+ 45 - 0
tests/Symfony/Tests/Component/Templating/EngineTest.php

@@ -131,6 +131,51 @@ class EngineTest extends \PHPUnit_Framework_TestCase
         $engine->setCharset('ISO-8859-1');
         $this->assertEquals('ISO-8859-1', $engine->getCharset(), '->setCharset() changes the default charset to use');
     }
+
+    public function testGlobalVariables()
+    {
+        $engine = new ProjectTemplateEngine(self::$loader);
+        $engine->addGlobal('global_variable', 'lorem ipsum');
+
+        $this->assertEquals(array(
+            'global_variable' => 'lorem ipsum',
+        ), $engine->getGlobals());
+    }
+
+    public function testGlobalVariablesGetPassedFromHelpers()
+    {
+        $engine = new ProjectTemplateEngine(self::$loader);
+        $engine->set(new \SimpleHelper('value'));
+        $engine->addGlobal('global_variable', 'lorem ipsum');
+
+        $this->assertEquals(array(
+            'global_variable' => 'lorem ipsum',
+            'global_from_helper' => 'helper lorem ipsum',
+        ), $engine->getGlobals());
+    }
+
+    public function testEngineGlobalOverwritesHelperGlobals()
+    {
+        $engine = new ProjectTemplateEngine(self::$loader);
+        $engine->set(new \SimpleHelper('value'));
+        $engine->addGlobal('global_from_helper', 'engine wins');
+
+        $this->assertEquals(array(
+            'global_from_helper' => 'engine wins',
+        ), $engine->getGlobals());
+    }
+
+    public function testGlobalsGetPassedToTemplate()
+    {
+        $engine = new ProjectTemplateEngine(self::$loader);
+        $engine->addGlobal('global', 'global variable');
+
+        self::$loader->setTemplate('global.php', '<?php echo $global; ?>');
+
+        $this->assertEquals($engine->render('global:php'), 'global variable');
+
+        $this->assertEquals($engine->render('global:php', array('global' => 'overwritten')), 'overwritten');
+    }
 }
 
 class ProjectTemplateEngine extends Engine

+ 7 - 0
tests/Symfony/Tests/Component/Templating/Fixtures/SimpleHelper.php

@@ -20,4 +20,11 @@ class SimpleHelper extends Helper
     {
         return 'foo';
     }
+
+    public function getGlobals()
+    {
+        return array(
+            'global_from_helper' => 'helper lorem ipsum',
+        );
+    }
 }