asjdn
asjdn

Reputation: 63

smarty plugin return array

I am creating a plugin that returns a array to the template file. I can't seem to get the full array to be parsed to the template file.

plugin function.arraytest

function smarty_function_arraytest($params,Smarty_Internal_Template $template)
{
    $x = array("1"=>array("categories"=>"Action"));
     return $x
}

template file

{arraytest}

result array

I cannot seem to iterate through the array in the template file.

Any help would be very much appreciated.

Thanks

Upvotes: 4

Views: 3751

Answers (2)

pjumble
pjumble

Reputation: 16960

Anything returned by a smarty function gets output directly, the .tpl never actually gets the array, just the string value "Array". It has been a long time since I used Smarty so this may have changed, but I believe the standard way of doing it is like this:

function smarty_function_arraytest($params, $template)
{
    // check $params['out'] exists here
    $array = array("1"=>array("categories"=>"Action"));
    $template->assign($params['out'], $array);
}

then call it like:

{arraytest out="arraytestValues"}
{foreach from=$arraytestValues key=k item=v}
   <p>{$k}: {$v}</p>
{/foreach}

of course, because you have a multidimensional array you'll need to loop over it twice:

{arraytest out="arraytestValues"}
{foreach from=$arraytestValues key=k item=v}
   <p>{$k}: {$v}</p>
    {foreach from=$v key=sk item=sv}
        <p>{$sk}: {$sv}</p>
    {/foreach}
{/foreach}

Upvotes: 5

ZloyPotroh
ZloyPotroh

Reputation: 377

Maybe it help:

{assign var=arr value=arraytest}
{$arr....}

Then docs: http://www.smarty.net/docsv2/en/language.syntax.variables.tpl

Upvotes: 0

Related Questions