聪明 {include} 文件+标记 如果存在,否则回退到文件


Smarty {include} File+Tag If exists, otherwise fallback to File

我正在开发一个白标产品,并希望能够快速覆盖一些模板,但不是全部。

我的想法是拥有这样的东西,比如说index.tpl

{include file="header.tpl" tag=$whitelabelname} 
{include file="body.tpl" tag=$whitelabelname} 
{include file="footer.tpl" tag=$whitelabelname}

我希望能够,如果有办法的话,自动包含文件header-whitelabelname.tpl(如果存在),或者如果不存在,则包含header.tpl。

$tagTemplate = substr($template, 0, -4) . "-" . $tag . ".tpl";
if ( file_exists($tagTemplate) ) $template = $tagTemplate;

这样做的原因是替代方案需要将所有模板文件更改为每个包含上的大开关语句,最终会得到非常大和复杂的模板。

是否可以以某种方式扩展{include}指令以包含我的逻辑,而不是自己手动更改smarty_internal_compile_include.php。

谢谢

您可以通过实现 Smarty 资源插件来做到这一点。查看文档页面上的示例。您必须将file_exists()逻辑实现到第一个函数中。还要检查方法 register_resource() 了解如何使用插件。