Merge pull request #13717 from nijel/template-cleanup
Simplify Template API
This commit is contained in:
commit
44bed27877
@ -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
|
||||
*/
|
||||
@ -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);
|
||||
@ -90,106 +76,26 @@ class Template
|
||||
* 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())
|
||||
public static function get($name)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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) {
|
||||
@ -209,17 +115,12 @@ class Template
|
||||
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;
|
||||
|
||||
@ -1 +0,0 @@
|
||||
<?= $this->hello($variable);
|
||||
@ -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);
|
||||
}
|
||||
@ -53,53 +52,6 @@ class TemplateTest extends PmaTestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
||||
Loading…
Reference in New Issue
Block a user