我正在制作一个PHP wiki引擎,它对所有指向它的网站使用相同的模板。但是,某些网站具有自定义模板。如果存在,我可以让 Smarty 使用此自定义模板吗?
这是我的目录结构:
/web/wiki/templates <-- all templates here
/web/wiki/templates/wiki.domain.com <-- individual template
如何先在/web/wiki/templates/wiki.domain.com
中巧妙地使用模板进行wiki.domain.com
,如果该目录中不存在模板,则在/web/wiki/templates
中使用模板?
我可以为 Smarty 定义多个模板目录,并让它首先尝试从顶部目录中选择模板吗?如果我能做到这一点,我可以简单地更改模板目录的顺序:
/web/wiki/templates/wiki.domain.com
/web/wiki/templates
default_template_handler是一个回调,如果找不到模板,则会调用该回调。在单元测试中可以找到一些"示例"
来自 Smarty Docs,尝试:
// set multiple directoríes where templates are stored
$smarty->setTemplateDir(array(
'one' => './templates',
'two' => './templates_2',
'three' => './templates_3',
));
要扩展 Krister 的代码,如果你有很多可能的模板:
$possibleTemplates = array(
// ...
);
do {
$template = array_shift($possibleTemplates);
} while($template && !$smarty->template_exists($template));
if(!$template) {
// Handle error
}
$smarty->display($template);
我认为您不能在不同的模板上设置优先级,但我不确定。您可以做的是检查特定模板是否存在:
// check for if a special template exists
$template = 'default.tpl.php';
if ($smarty->template_exists('example.tpl.php')) {
$template = 'example.tpl.php';
}
// render the template
$smarty->display($template);