diff --git a/libraries/Template.php b/libraries/Template.php index bb0801726b..b2a759f3de 100644 --- a/libraries/Template.php +++ b/libraries/Template.php @@ -75,25 +75,21 @@ class Template return preg_replace($regexp, "$1$2", $content); } - /** - * Sets data to be used by this template - * - * @param array $data containing data entries - */ - public function setData($data = array()) - { - $this->data = $data; - } - /** * Adds more entries to the data for this template * - * @param array $data containing data entries + * @param array|string $data containing data array or data key + * @param string $value containing data value */ - public function addData($data = array()) + public function set($data, $value = null) { - foreach ($data as $key => $value) { - $this->data[$key] = $value; + if(is_array($data) && ! $value) { + $this->data = array_merge( + $this->data, + $data + ); + } else if (is_string($data)) { + $this->data[$data] = $value; } } @@ -103,7 +99,7 @@ class Template * @param string $funcName function name * @param callable $funcDef function definition */ - public function addFunction($funcName, $funcDef) + public function setHelper($funcName, $funcDef) { if (! isset($this->helperFunctions[$funcName])) { $this->helperFunctions[$funcName] = $funcDef; @@ -119,7 +115,7 @@ class Template * * @param string $funcName function name */ - public function removeFunction($funcName) + public function removeHelper($funcName) { if (isset($this->helperFunctions[$funcName])) { unset($this->helperFunctions[$funcName]); @@ -160,7 +156,7 @@ class Template { $template = static::BASE_PATH . $this->name . '.phtml'; try { - $this->addData($data); + $this->set($data); extract($this->data); $this->helperFunctions = array_merge( $this->helperFunctions, diff --git a/templates/test/add_function.phtml b/templates/test/set_helper.phtml similarity index 100% rename from templates/test/add_function.phtml rename to templates/test/set_helper.phtml diff --git a/test/classes/TemplateTest.php b/test/classes/TemplateTest.php index a9345e9051..8d9a19aa67 100644 --- a/test/classes/TemplateTest.php +++ b/test/classes/TemplateTest.php @@ -16,35 +16,15 @@ require_once 'test/PMATestCase.php'; class TemplateTest extends PMATestCase { /** - * Test for setData + * Test for set function * * @return void */ - public function testSetData() - { - $template = PMA\libraries\Template::get('test/echo'); - $template->setData( - array( - 'variable' => 'value' - ) - ); - $this->assertEquals('value', $template->render()); - } - - /** - * Test for addData - * - * @return void - */ - public function testAddData() + public function testSet() { $template = PMA\libraries\Template::get('test/add_data'); - $template->addData( - array( - 'variable1' => 'value1' - ) - ); - $template->addData( + $template->set('variable1', 'value1'); + $template->set( array( 'variable2' => 'value2' ) @@ -55,17 +35,17 @@ class TemplateTest extends PMATestCase } /** - * Test for addFunction + * Test for setHelper * * @return void */ - public function testAddFunction() + public function testSetHelper() { - $template = PMA\libraries\Template::get('test/add_function'); - $template->addFunction('hello', function ($string) { + $template = PMA\libraries\Template::get('test/set_helper'); + $template->setHelper('hello', function ($string) { return 'hello ' . $string; }); - $template->addData( + $template->set( array( 'variable' => 'world' )