Skip to content

Commit

Permalink
Merge pull request #13717 from nijel/template-cleanup
Browse files Browse the repository at this point in the history
Simplify Template API
  • Loading branch information
nijel committed Oct 3, 2017
2 parents d7d3b57 + fd1705f commit 44bed27
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 157 deletions.
111 changes: 6 additions & 105 deletions libraries/classes/Template.php
Expand Up @@ -31,16 +31,6 @@ class Template
*/
protected $name = null;

/**
* Data associated with the template
*/
protected $data;

/**
* Helper functions for the template
*/
protected $helperFunctions;

/**
* Twig environment
*/
Expand All @@ -52,16 +42,12 @@ class Template
* 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, array $data = array(), array $helperFunctions = array())
protected function __construct($name)
{
static $twig = null;

$this->name = $name;
$this->data = $data;
$this->helperFunctions = $helperFunctions;

if (is_null($twig)) {
$loader = new Twig_Loader_Filesystem(static::BASE_PATH);
Expand Down Expand Up @@ -90,106 +76,26 @@ protected function __construct($name, array $data = array(), array $helperFuncti
* 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, array $data = array(), array $helperFunctions = array())
{
return new Template($name, $data, $helperFunctions);
}

/**
* 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
*
* @return void
*/
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
*
* @return void
*/
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
*
* @return void
*/
public function removeHelper($funcName)
public static function get($name)
{
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
*
* @return mixed
*/
public function __call($funcName, array $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.'
);
}
return new Template($name);
}

/**
* Render template
*
* @param array $data Variables to be provided to the template
* @param array $helperFunctions Helper functions to be used by template
*
* @return string
*/
public function render(array $data = array(), array $helperFunctions = array())
public function render(array $data = array())
{
$template = static::BASE_PATH . $this->name;

if (file_exists($template . '.twig')) {
$this->set($data);
try {
$template = $this->twig->load($this->name . '.twig');
} catch (\RuntimeException $e) {
Expand All @@ -209,17 +115,12 @@ public function render(array $data = array(), array $helperFunctions = array())
E_USER_WARNING
);
}
return $template->render($this->data);
return $template->render($data);
}

$template = $template . '.phtml';
try {
$this->set($data);
$this->helperFunctions = array_merge(
$this->helperFunctions,
$helperFunctions
);
extract($this->data);
extract($data);
ob_start();
if (@file_exists($template)) {
include $template;
Expand Down
1 change: 0 additions & 1 deletion templates/test/set_helper.phtml

This file was deleted.

54 changes: 3 additions & 51 deletions test/classes/TemplateTest.php
Expand Up @@ -29,13 +29,12 @@ class TemplateTest extends PmaTestCase
public function testSet($data)
{
$template = Template::get($data);
$template->set('variable1', 'value1');
$template->set(
$result = $template->render(
array(
'variable2' => 'value2'
'variable1' => 'value1',
'variable2' => 'value2',
)
);
$result = $template->render();
$this->assertContains('value1', $result);
$this->assertContains('value2', $result);
}
Expand All @@ -53,53 +52,6 @@ public function providerTestSet()
];
}

/**
* Test for setHelper
*
* @return void
*/
public function testSetHelper()
{
$template = Template::get('test/set_helper');
$template->setHelper('hello', function ($string) {
return 'hello ' . $string;
});
$template->set(['variable' => 'world']);
$this->assertEquals('hello world', $template->render());

$this->setExpectedException('LogicException');
$template->setHelper('hello', 'again');
}

/**
* Test for removeHelper
*
* @return void
*/
public function testRemoveHelper()
{
$template = Template::get('test/set_helper');
$template->setHelper('hello', function ($string) {
return 'hello ' . $string;
});
$template->set(['variable' => 'world']);
$template->removeHelper('hello');
$this->setExpectedException('LogicException');
$template->render();
}

/**
* Test for removeHelper
*
* @return void
*/
public function testRemoveHelperNotFound()
{
$template = Template::get('test/set_helper');
$this->setExpectedException('LogicException');
$template->removeHelper('not found');
}

/**
* Test for render
*
Expand Down

0 comments on commit 44bed27

Please sign in to comment.