如何为每个智能模板添加相同的前缀

时间:2012-06-06 08:04:30

标签: smarty

每次包含tpl文件时,系统首先查找该文件的特定于站点的版本,如果特定于站点的文件不存在,则回退到标准版本。所以我提出的perah包括“customer / main / test1.tpl”。如果我们的网站是谷歌,系统将首先查找“customer / main / google_test1.tpl”,如果该文件不存在,则回退到“customer / main / test1.tpl”。

注意:Smarty 2.6.x

1 个答案:

答案 0 :(得分:1)

您是否了解内置模板目录级联? addTemplateDirsetTemplateDir允许您指定多个目录:

$smarty->setTemplateDir(array(
  'google' => 'my-templates/google/',
  'default' => 'my-templates/default/',
));

$smarty->display('foobar.tpl');

Smarty将首先尝试查找my-templates/google/foobar.tpl,如果找不到,请尝试my-templates/default/foobar.tpl。使用它,您可以构建一个完整的模板级联。


如果你在同一级联级别上有很多元素,这将不会太有用。假设除了你的default.tpl之外你还有google,yahoo和bing的特定模板。解决方案可能涉及default template handler function。每当Smarty遇到无法找到的模板时,此回调将作为最后的手段执行。它允许您指定模板文件(或模板资源)以用作后备。

所以你可以{include},{extend}, - > fetch(), - > display()site_google.tpl。如果文件存在,一切都很好。如果没有,您的回调可以将_google替换为_default以回退到默认模板。


如果template_dir级联和默认模板处理函数都不适用,那么你需要深入研究custom template resources