繁体   English   中英

如何使用Smarty模板中的Smarty模板?

[英]How can I use a Smarty template from a Smarty template?

我遇到了一个有趣的问题。 我需要在Smarty模板使用Smarty模板。

这就是原因。 我为各种wiki网站使用相同的模板,每个网站都有自己的配置。 配置包含主模板的部分(例如更改的标题和标题等)。

这是一个简化的例子。 我有一个文件topic-list.template.html ,它在所有网站上共享:

<div id="topics">
  <h1>{$h1}</h1>
  ...
</div>

如您所见,此模板文件包含可为每个网站自定义的<h1>标记。

然后,对于每个网站,我都有一个看起来像这样的配置文件(简化):

$config = [
  "h1-titles" => [
      "topics" => "Showing Topics in {\$category}"
  ]
];

如您所见,配置文件包含Smarty模板。

因此,当我渲染topic-list.template.html文件时,我首先通过$smarty->fetch("string":$config['h1-titles']['topics'])渲染$config['h1-titles']['topics'] $smarty->fetch("string":$config['h1-titles']['topics']) ,然后将其分配给h1 Smarty变量。 我的简化代码如下所示:

$h1_tag = $smarty->fetch("string":$config['h1-titles']['topics']);
$smarty->assign('h1', $h1_tag);
$smarty->display('topic-list.template.html');

我想知道我是否能够以某种方式自动在topic-list.template.html文件中插入$config['h1-titles']['topics'] ,然后一次性全部渲染?

请查看字符串模板资源的文档。 你会立即注意到你的$smarty->fetch('string:…')方法也可以在模板中完成: {include file="string:…"}

我相信{eval}标签可以帮助您:

{eval}用于将变量计算为模板。 这可以用于将模板标签/变量嵌入变量或标签/变量到配置文件变量中。

http://www.smarty.net/docs/en/language.function.eval.tpl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM