4 * Smarty Method RegisterDefaultTemplateHandler
6 * Smarty::registerDefaultTemplateHandler() method
9 * @subpackage PluginsInternal
12 class Smarty_Internal_Method_RegisterDefaultTemplateHandler
15 * Valid for Smarty and template object
22 * Register template default handler
24 * @api Smarty::registerDefaultTemplateHandler()
26 * @param \Smarty_Internal_TemplateBase|\Smarty_Internal_Template|\Smarty $obj
27 * @param callable $callback class/method name
29 * @return \Smarty|\Smarty_Internal_Template
30 * @throws SmartyException if $callback is not callable
32 public function registerDefaultTemplateHandler(Smarty_Internal_TemplateBase $obj, $callback)
34 $smarty = $obj->_getSmartyObj();
35 if (is_callable($callback)) {
36 $smarty->default_template_handler_func = $callback;
38 throw new SmartyException("Default template handler not callable");
44 * get default content from template or config resource handler
46 * @param Smarty_Template_Source $source
48 * @throws \SmartyException
50 public static function _getDefaultTemplate(Smarty_Template_Source $source)
52 if ($source->isConfig) {
53 $default_handler = $source->smarty->default_config_handler_func;
55 $default_handler = $source->smarty->default_template_handler_func;
57 $_content = $_timestamp = null;
58 $_return = call_user_func_array($default_handler,
59 array($source->type, $source->name, &$_content, &$_timestamp, $source->smarty));
60 if (is_string($_return)) {
61 $source->exists = is_file($_return);
62 if ($source->exists) {
63 $source->timestamp = filemtime($_return);
65 throw new SmartyException("Default handler: Unable to load " .
66 ($source->isConfig ? 'config' : 'template') .
67 " default file '{$_return}' for '{$source->type}:{$source->name}'");
69 $source->name = $source->filepath = $_return;
70 $source->uid = sha1($source->filepath);
71 } elseif ($_return === true) {
72 $source->content = $_content;
73 $source->exists = true;
74 $source->uid = $source->name = sha1($_content);
75 $source->handler = Smarty_Resource::load($source->smarty, 'eval');
77 $source->exists = false;
78 throw new SmartyException('Default handler: No ' . ($source->isConfig ? 'config' : 'template') .
79 " default content for '{$source->type}:{$source->name}'");