tags:

views:

543

answers:

4

Is it possible 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?

A: 

Hi, 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.

jkndrkn
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=blaurl=blaprice=bladescription=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_load file='product.conf' section=$current}{$smarty.config.client}{$smarty.config.url}{$smarty.config.price}{$smarty.config.description}{/foreach}
quardas
A: 

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}
quardas
A: 

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.

malonso
A: 

Someone at the smarty forums found a way. See:

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

Christopher Mahan