简体   繁体   中英

Using plugin functions in Smarty foreach

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!

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

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM