Improve template loader

This commit is contained in:
Frédéric Guillot
2014-09-16 13:35:39 +02:00
parent 12a688347c
commit b1ffbbd501

View File

@@ -2,6 +2,8 @@
namespace Core; namespace Core;
use LogicException;
/** /**
* Template class * Template class
* *
@@ -25,31 +27,22 @@ class Template
* $template->load('template_name', ['bla' => 'value']); * $template->load('template_name', ['bla' => 'value']);
* *
* @access public * @access public
* @params string $__template_name Template name
* @params array $__template_args Key/Value map of template variables
* @return string * @return string
*/ */
public function load() public function load($__template_name, array $__template_args = array())
{ {
if (func_num_args() < 1 || func_num_args() > 2) { $__template_file = self::PATH.$__template_name.'.php';
die('Invalid template arguments');
if (! file_exists($__template_file)) {
throw new LogicException('Unable to load the template: "'.$__template_name.'"');
} }
if (! file_exists(self::PATH.func_get_arg(0).'.php')) { extract($__template_args);
die('Unable to load the template: "'.func_get_arg(0).'"');
}
if (func_num_args() === 2) {
if (! is_array(func_get_arg(1))) {
die('Template variables must be an array');
}
extract(func_get_arg(1));
}
ob_start(); ob_start();
include $__template_file;
include self::PATH.func_get_arg(0).'.php';
return ob_get_clean(); return ob_get_clean();
} }