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!

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

This looks great, but is there any way to prevent the first line of code? Like, embedding that into the foreach? Ty!
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
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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.