Php 我可以让Smarty根据优先级从目录中选择模板吗?
我正在制作一个PHP wiki引擎,它对指向它的所有网站使用相同的模板。但是,有些网站有一个自定义模板。如果存在此自定义模板,我可以让Smarty使用它吗 以下是我的目录结构:Php 我可以让Smarty根据优先级从目录中选择模板吗?,php,templates,smarty,Php,Templates,Smarty,我正在制作一个PHP wiki引擎,它对指向它的所有网站使用相同的模板。但是,有些网站有一个自定义模板。如果存在此自定义模板,我可以让Smarty使用它吗 以下是我的目录结构: /web/wiki/templates <-- all templates here /web/wiki/templates/wiki.domain.com <-- individual template 我不认为您可以设置不同模板的优先级,但我不确定。您可以做的是检查是否
/web/wiki/templates <-- all templates here
/web/wiki/templates/wiki.domain.com <-- individual 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);
要扩展Krister的代码,如果您有许多可能的模板:
$possibleTemplates = array(
// ...
);
do {
$template = array_shift($possibleTemplates);
} while($template && !$smarty->template_exists($template));
if(!$template) {
// Handle error
}
$smarty->display($template);
是在找不到模板时调用的回调。一些“示例”可以从中找到,请尝试:
好办法,我试试这个。我想我也会尝试我的方法,看看我是否可以设置多个模板目录,然后我会看看哪一个优先,如果我可以的话。(我还没有试过。)@bodacydo-如果你知道怎么做,请告诉我。+1-不知道这个方法,这似乎是一个很好的解决方案,而不是使用
template\u exists
检查具有模板名称的数组,至于优先级,“/templates”将收到最高的,如果没有找到模板,它将在“./templates\u 2”中搜索,然后在“./templates\u 3”中搜索
$possibleTemplates = array(
// ...
);
do {
$template = array_shift($possibleTemplates);
} while($template && !$smarty->template_exists($template));
if(!$template) {
// Handle error
}
$smarty->display($template);
// set multiple directoríes where templates are stored
$smarty->setTemplateDir(array(
'one' => './templates',
'two' => './templates_2',
'three' => './templates_3',
));