[英]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.. 我最近一直在试验Smarty(第一次使用这种东西),但是我有一个快速的问题,我想不通。
I have created a function for Smarty, called get_users() , so it'd be {get_users} into my .tpl 我为Smarty创建了一个名为get_users()的函数,因此它将{get_users}放入我的.tpl中。
I want to do a foreach of these "get_users", so it'd look like this 我想对这些“ get_users”做一个foreach,所以看起来像这样
{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: 您可能应该在函数内使用$ smarty-> assign(...)将结果返回到变量中,然后编写如下内容:
{get_users var=user_list}
{foreach $user_list as $user}
....
{/foreach}
read http://www.smarty.net/docs/en/plugins.functions.tpl 阅读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}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.