Smarty 是什么?
为何使用它?
使用案例和工作流程
语法比较
模板继承
最佳实践
速成课程
当无法从资源中获取模板时,会调用此函数。
目前,默认处理程序仅对文件资源进行调用。当资源本身未找到时不会触发,在这种情况下,会抛出 SmartyException。
示例 13.5. $default_template_handler_func
<?php $smarty = new Smarty(); $smarty->default_template_handler_func = 'my_default_template_handler_func'; /** * Default Template Handler * * called when Smarty's file: resource is unable to load a requested file * * @param string $type resource type (e.g. "file", "string", "eval", "resource") * @param string $name resource name (e.g. "foo/bar.tpl") * @param string &$content template's content * @param integer &$modified template's modification time * @param Smarty $smarty Smarty instance * @return string|boolean path to file or boolean true if $content and $modified * have been filled, boolean false if no default template * could be loaded */ function my_default_template_handler_func($type, $name, &$content, &$modified, Smarty $smarty) { if (false) { // return corrected filepath return "/tmp/some/foobar.tpl"; } elseif (false) { // return a template directly $content = "the template source"; $modified = time(); return true; } else { // tell smarty that we failed return false; } } ?>