Smarty - 捕获“无法加载模板”错误

时间:2014-01-03 19:53:48

标签: php smarty

我使用get参数自动加载.tpl文件。

参数可能有误,下次移动时会因错误“Smarty:无法加载模板”而停止。

我可以捕获此错误并将页面重定向到某个默认模板吗?

非常感谢!

$seo = $params[0];
$smarty->display($seo . '.tpl');

1 个答案:

答案 0 :(得分:1)

Smarty具有检查模板是否存在的功能。

来自documentation

if( !$smarty->template_exists($mid_template) ){
    $mid_template = 'page_not_found.tpl';
}

你可以这样使用它:

if($smarty->template_exists($template)) {
    header("Location: error.html");
    exit();
}
$smarty->display($template);