Php 通过配置文件在smarty中定义阵列

Php 通过配置文件在smarty中定义阵列,php,smarty,Php,Smarty,是否可以在smarty中的配置文件中定义数组??例如,我希望在配置文件(位于/configs中)中有一个小的数据库—几个(大约20个)产品描述:标题、价格、描述。之后,我想通过foreach或section列出它。 如果没有MySql或其他db引擎,我如何在Smarty中定义该数组。我可以这样做吗?我查看了Smarty文档,特别是第页 我在这里没有看到初始化数组的示例,我怀疑Smarty不支持在其配置文件中初始化数组,因为配置文件似乎是简单的键值存储。您当然可以尝试在配置文件中初始化一个数组,并

是否可以在smarty中的配置文件中定义数组??例如,我希望在配置文件(位于/configs中)中有一个小的数据库—几个(大约20个)产品描述:标题、价格、描述。之后,我想通过foreach或section列出它。
如果没有MySql或其他db引擎,我如何在Smarty中定义该数组。我可以这样做吗?

我查看了Smarty文档,特别是第页

我在这里没有看到初始化数组的示例,我怀疑Smarty不支持在其配置文件中初始化数组,因为配置文件似乎是简单的键值存储。您当然可以尝试在配置文件中初始化一个数组,并告诉我们它是否适合您


在其他Smarty文档页面上,初始化数组的首选位置似乎是在.php页面中,该页面用于初始化值,并在显示之前将其加载到模板中。

再次,因为格式化:


这似乎是真的。不可能在那里定义数组。我什么都试过了。。最后,我使用了控制器分配数组(如您所建议的):

php:

product.conf

[1]
client=
url=
price=
description=

[2]
client=
url=
price=
description=

[3]
client=
url=
price=
description=

[4]
client=
url=
price=
description=
第三方物流:


是否有任何理由需要在smarty中执行此操作?如果您只是在PHP中创建数组并将其传递给smarty变量,那么它看起来可能更适合未来。这样,如果您离开smarty,所有数据仍然可以访问,无需重新编码。

smarty论坛的某个人找到了一种方法。见:


您可以在配置文件中定义数组,您需要设置$config\u overwrite=FALSE。 您可以查看此页面:

配置文件

   # row colors
rowColors = #FF0000
rowColors = #00FF00
rowColors = #0000FF
具有{section}循环的模板

 <table>
  {section name=r loop=$rows}
  <tr bgcolor="{cycle values=#rowColors#}">
    <td> ....etc.... </td>
  </tr>
  {/section}
</table>

{section name=r loop=$rows}
等
{/section}

这似乎是真的。不可能在那里定义数组。我什么都试过了。。最后,我使用了控制器中的assign数组(如您所建议的):php:$smarty->assign('list',array(1,2,3,4));product.conf[1]client=bla url=bla price=bla description=bla[2]client=url=price=description=[3]client=url=price=description=[4]client=url=price=description=tpl:{foreach from=$list item=current name=prod}{config\u load file='product.conf'section=$current}{$smarty.config.client}{$smarty.config.url}{$smarty.config.price}{$smarty.config.description}{/foreach}
$smarty = new Smarty;
$smarty->config_overwrite=false; 
   # row colors
rowColors = #FF0000
rowColors = #00FF00
rowColors = #0000FF
 <table>
  {section name=r loop=$rows}
  <tr bgcolor="{cycle values=#rowColors#}">
    <td> ....etc.... </td>
  </tr>
  {/section}
</table>