Reputation: 1239
In a smarty template I call a user defined function, as a modifier like that:
{"myArray"|assignArray}
my user defined function in php looks as:
function smarty_modifier_assignArray($str)
{
global $smarty;
if ($str=="myArray")
{
// it is not constant in real, but comes from a mysql query
$all = array( array("foo","joe")), array("green", "blue"));
$smarty->assign($str,$all);
}
return null;
}
My purpose is that loading "myArray" from mysql is expensive, and if my template do not need that array, I don't want to load it. My template follows as:
{"myArray"|assignArray}
{foreach from=$myArray item=r}
{$r[0]}
{/foreach}
The problem is, that in the foreach I can't see $myArray (or it is empty). I read in the smarty forum (v3.x, http://www.smarty.net/forums/viewtopic.php?p=77671 ) that for speed optimalization the variables are copied to the template space, so after starting a template, one can not assign new variables onto that. Sadly it seems true. I suppose using template {assign ...} I still could do that, but I was not able to generate dynamical multi-level arrays into that {assign ...} :(
What should I do? Any ideas?
Upvotes: 0
Views: 3056
Reputation: 86
You can assign smarty multilevel array while rendering, use {assign} tag for it.
Syntax,
{assign var=foo value=[1,[9,8],3]} // can be nested
Upvotes: 2