开发者

defining arrays in smarty via config files

开发者 https://www.devze.com 2022-12-11 06:50 出处:网络
Is it poss开发者_开发问答ible to defining arrays in config files in smarty?? for example I want have small data base in config file (located in /configs) - few (about 20) products descriptions: title,

Is it poss开发者_开发问答ible to defining arrays in config files in smarty?? for example I want have small data base in config file (located in /configs) - few (about 20) products descriptions: title, price, description. After that I want to list it via foreach or section. How can I define that array in Smarty without MySql or other db engine. Can I do that?


You can define arrays in config file, you need to set $config_overwrite = FALSE. you can look at this page: config_overwrite

$smarty = new Smarty;
$smarty->config_overwrite=false; 

The config file.

   # row colors
rowColors = #FF0000
rowColors = #00FF00
rowColors = #0000FF

The template with a {section} loop.

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


Someone at the smarty forums found a way. See:

http://www.smarty.net/forums/viewtopic.php?p=61528


I took a look at Smarty documentation, specifically this page.

I don't see an example of an array being initialized here and I suspect that Smarty does not support initialization of arrays in its configuration files, as the configuration files appear to be simple key-value stores. You could certainly try to initialize an array in a configuration file and let us know if it works for you.

On other Smarty documentation pages, it looks like the preferred place for initializing an array is in the .php page that is used to initialize values and load them into a template prior to displaying it.


once again, because of formating:

It seems to be true. There is no possibility to define arrays there. I tried everything.. at long last i used assign array from controller (as You suggested):

php:

$smarty->assign('list', array(1,2,3,4)); 

product.conf

[1]
client=
url=
price=
description=

[2]
client=
url=
price=
description=

[3]
client=
url=
price=
description=

[4]
client=
url=
price=
description=

tpl:

{foreach from=$list item=current name=prod}
{config_load file='product.conf' section=$current}

{$smarty.config.client}
{$smarty.config.url}
{$smarty.config.price}
{$smarty.config.description}

{/foreach}


Is there any reason that this NEEDS to be done in smarty? It seems like it might be more future-proof if you just created the array in PHP and pass to a smarty variable. This way if you ever move away from smarty, all the data is still accessible w/out any recoding.

0

精彩评论

暂无评论...
验证码 换一张
取 消