[Templating] tweaked previous commit
This commit is contained in:
parent
afc2f96549
commit
ed359af543
@ -52,6 +52,7 @@ class Engine implements \ArrayAccess
|
||||
$this->stack = array();
|
||||
$this->charset = 'UTF-8';
|
||||
$this->cache = array();
|
||||
$this->globals = array();
|
||||
|
||||
$this->addHelpers($helpers);
|
||||
|
||||
@ -407,27 +408,16 @@ class Engine implements \ArrayAccess
|
||||
*/
|
||||
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.
|
||||
* Returns the assigned globals.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
@ -42,14 +42,4 @@ abstract class Helper implements HelperInterface
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get global variables for templates
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGlobals()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
@ -38,12 +38,4 @@ 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();
|
||||
}
|
||||
|
@ -142,29 +142,6 @@ class EngineTest extends \PHPUnit_Framework_TestCase
|
||||
), $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);
|
||||
|
@ -20,11 +20,4 @@ class SimpleHelper extends Helper
|
||||
{
|
||||
return 'foo';
|
||||
}
|
||||
|
||||
public function getGlobals()
|
||||
{
|
||||
return array(
|
||||
'global_from_helper' => 'helper lorem ipsum',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user