diff --git a/libraries/Template.php b/libraries/Template.php index 8a1a3aa83e..b2a759f3de 100644 --- a/libraries/Template.php +++ b/libraries/Template.php @@ -16,31 +16,49 @@ namespace PMA\libraries; */ class Template { - + /** + * Name of the template + */ protected $name = null; + /** + * Data associated with the template + */ + protected $data; + + /** + * Helper functions for the template + */ + protected $helperFunctions; + const BASE_PATH = 'templates/'; /** * Template constructor * * @param string $name Template name + * @param array $data Variables to be provided to the template + * @param array $helperFunctions Helper functions to be used by template */ - protected function __construct($name) + protected function __construct($name, $data = array(), $helperFunctions = array()) { $this->name = $name; + $this->data = $data; + $this->helperFunctions = $helperFunctions; } /** * Template getter * * @param string $name Template name + * @param array $data Variables to be provided to the template + * @param array $helperFunctions Helper functions to be used by template * * @return Template */ - public static function get($name) + public static function get($name, $data = array(), $helperFunctions = array()) { - return new Template($name); + return new Template($name, $data, $helperFunctions); } /** @@ -57,19 +75,93 @@ class Template return preg_replace($regexp, "$1$2", $content); } + /** + * Adds more entries to the data for this template + * + * @param array|string $data containing data array or data key + * @param string $value containing data value + */ + public function set($data, $value = null) + { + if(is_array($data) && ! $value) { + $this->data = array_merge( + $this->data, + $data + ); + } else if (is_string($data)) { + $this->data[$data] = $value; + } + } + + /** + * Adds a function for use by the template + * + * @param string $funcName function name + * @param callable $funcDef function definition + */ + public function setHelper($funcName, $funcDef) + { + if (! isset($this->helperFunctions[$funcName])) { + $this->helperFunctions[$funcName] = $funcDef; + } else { + throw new \LogicException( + 'The function "' . $funcName . '" is already associated with the template.' + ); + } + } + + /** + * Removes a function + * + * @param string $funcName function name + */ + public function removeHelper($funcName) + { + if (isset($this->helperFunctions[$funcName])) { + unset($this->helperFunctions[$funcName]); + } else { + throw new \LogicException( + 'The function "' . $funcName . '" is not associated with the template.' + ); + } + } + + /** + * Magic call to locally inaccessible but associated helper functions + * + * @param string $funcName function name + * @param array $arguments function arguments + */ + public function __call($funcName, $arguments) + { + if (isset($this->helperFunctions[$funcName])) { + return call_user_func_array($this->helperFunctions[$funcName], $arguments); + } else { + throw new \LogicException( + 'The function "' . $funcName . '" is not associated with the template.' + ); + } + } + /** * Render template * - * @param array $data Variables to provides for template + * @param array $data Variables to be provided to the template * @param bool $trim Trim content + * @param array $helperFunctions Helper functions to be used by template * * @return string */ - public function render($data = array(), $trim = true) + public function render($data = array(), $trim = true, $helperFunctions = array()) { $template = static::BASE_PATH . $this->name . '.phtml'; try { - extract($data); + $this->set($data); + extract($this->data); + $this->helperFunctions = array_merge( + $this->helperFunctions, + $helperFunctions + ); ob_start(); if (file_exists($template)) { include $template; diff --git a/templates/test/add_data.phtml b/templates/test/add_data.phtml new file mode 100644 index 0000000000..eee5ebe053 --- /dev/null +++ b/templates/test/add_data.phtml @@ -0,0 +1,3 @@ +hello($variable); diff --git a/test/classes/TemplateTest.php b/test/classes/TemplateTest.php index 69a148a903..8d9a19aa67 100644 --- a/test/classes/TemplateTest.php +++ b/test/classes/TemplateTest.php @@ -15,6 +15,44 @@ require_once 'test/PMATestCase.php'; */ class TemplateTest extends PMATestCase { + /** + * Test for set function + * + * @return void + */ + public function testSet() + { + $template = PMA\libraries\Template::get('test/add_data'); + $template->set('variable1', 'value1'); + $template->set( + array( + 'variable2' => 'value2' + ) + ); + $result = $template->render(); + $this->assertContains('value1', $result); + $this->assertContains('value2', $result); + } + + /** + * Test for setHelper + * + * @return void + */ + public function testSetHelper() + { + $template = PMA\libraries\Template::get('test/set_helper'); + $template->setHelper('hello', function ($string) { + return 'hello ' . $string; + }); + $template->set( + array( + 'variable' => 'world' + ) + ); + $this->assertEquals('hello world', $template->render()); + } + /** * Test for render *