提供一个回调函数,这个回调函数将在从资源里面无法获取到模板的时候调用。
该设置一般在当模板是文件资源的时候起作用。 当找不到资源自身的时候,该设置不会调用,而是会抛出SmartyException异常。
Example 13.5. $default_template_handler_func
<?php
$smarty = new Smarty();
$smarty->default_template_handler_func = 'my_default_template_handler_func';
/**
* 默认模板处理函数
*
* 当Smarty的文件资源无法载入需要的文件时调用。
*
* @param string $type 资源类型 (如:"file", "string", "eval", "resource")
* @param string $name 资源名称 (如:"foo/bar.tpl")
* @param string &$content 模板的内容
* @param integer &$modified 模板修改时间
* @param Smarty $smarty Smarty实例
* @return string|boolean 返回文件路径或布尔值,
* 布尔值true表示$content 和 $modified已经赋值,
* 布尔值false表示没有默认模板可供载入。
*/
function my_default_template_handler_func($type, $name, &$content, &$modified, Smarty $smarty) {
if (false) {
// 返回文件路径
return "/tmp/some/foobar.tpl";
} elseif (false) {
// 直接返回模板内容
$content = "the template source";
$modified = time();
return true;
} else {
// 告诉Smarty无法找到模板
return false;
}
}
?>