1

I've been experimenting with Smarty lately a bit (first time into using this kind of stuff), and I have a quick question I just can't figure out..

I have created a function for Smarty, called get_users(), so it'd be {get_users} into my .tpl

I want to do a foreach of these "get_users", so it'd look like this

{foreach get_users as $user}
magic
{/foreach}

Now, my question is.. as this is not working, how should I approach this issue?

Thanks!

Mauro Casas
  • 2,235
  • 1
  • 13
  • 15

2 Answers2

0

You probably should use $smarty->assign(...) inside the function to return the result in a variable and then write something like:

{get_users var=user_list}

{foreach $user_list as $user}
....
{/foreach}

read http://www.smarty.net/docs/en/plugins.functions.tpl

Borgtex
  • 3,235
  • 1
  • 19
  • 28
  • This looks great, but is there any way to prevent the first line of code? Like, embedding that into the foreach? Ty! – Mauro Casas Oct 22 '13 at 17:34
  • Not that I know. Maybe using the function as a modifier with an empty variable, like {foreach $tmp|user_list as $user}, but the other way looks much cleaner and it's probably faster – Borgtex Oct 23 '13 at 07:14
0

First, your plugin should assign the users variable to the template before iterating over it. This can be done like this :

function smarty_function_get_users($params, $smarty)
{
    ..... // your stuff goes here       
    $users = array(); // get your users data here
    $smarty->assign($params['users'], $users); 
}

Then you can iterate over it like this :

{get_users users=users}
{foreach from=$users item=user}
    {$user}
{/foreach}
Fouad Fodail
  • 2,653
  • 1
  • 16
  • 16