mapo mapo - 4 months ago 33
Smarty Question

Translate variable name on Smarty foreach loop with gettext

I've a problem with Smarty 3 foreach loop and gettext translations.
In my controller i intercept the variables from a MySQL database and then i create an array:

CONTROLLER:

array_categories[] = array(
'name_cat_it' => $name_cat_it,
'name_cat_en' => $name_cat_en,
);
$smarty->assign('array_cat', $array_categories);


TEMPLATE:

{foreach name=categorie key=key item=value from=$array_cat}
{$value.name_cat_it}
{/foreach}


Till now everything is ok, but i would like to do something like this:

{foreach name=categorie key=key item=value from=$array_cat}
{$value.name_cat_{$lang}}
{/foreach}


$lang can be 'it' o 'en'.
Is this possible inside a foreach loop?
Outside the loop i have no problem doing this for the meta title or the meta description.

Thanks a lot!

M

Answer

Yes, you can use a temporal variable to compose the name:

{$name_lang='name_cat_'|cat:$lang}    
{foreach name=categorie key=key item=value from=$array_cat}
{$value.$name_lang}
{/foreach}
Comments