4

我正在制作一个 PHP wiki 引擎,它对指向它的所有网站都使用相同的模板。但是,有些网站有自定义模板。如果它存在,我可以让 Smarty 使用这个自定义模板吗?

这是我的目录结构:

/web/wiki/templates                 <--  all templates here
/web/wiki/templates/wiki.domain.com <-- individual template

我怎样才能使 smarty 在/web/wiki/templates/wiki.domain.com第一个 for中使用模板wiki.domain.com,如果此目录中不存在该模板,则在 中使用该模板/web/wiki/templates

我可以为 Smarty 定义多个模板目录,并让它首先尝试从顶层目录中选择模板吗?如果我能做到这一点,我可以简单地改变模板目录的顺序:

/web/wiki/templates/wiki.domain.com
/web/wiki/templates                
4

4 回答 4

1

Smarty Docs尝试:

// set multiple directoríes where templates are stored
$smarty->setTemplateDir(array(
    'one'   => './templates',
    'two'   => './templates_2',
    'three' => './templates_3',
));
于 2014-01-30T14:52:39.700 回答
1

default_template_handler是在找不到模板时调用的回调。在单元测试中可以找到一些“示例”

于 2012-06-17T08:20:23.033 回答
0

要扩展 Krister 的代码,如果您有很多可能的模板:

$possibleTemplates = array(
    // ...
);

do {
    $template = array_shift($possibleTemplates);
} while($template && !$smarty->template_exists($template));

if(!$template) {
    // Handle error
}

$smarty->display($template);
于 2012-06-16T23:37:51.003 回答
0

我认为您不能在不同的模板上设置优先级,但我不确定。您可以做的是检查是否存在特定模板:

// 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);
于 2012-06-16T23:21:30.810 回答