急:在Smarty模板循环体中调用自定义函数

dgseamaple 2010-08-13 03:42:26
//注册函数
$smarty->register_function('getphotopath', 'smarty_photo_path');

//函数
function smarty_photo_path($params)
{
extract($params);
$id = str_pad($id, 9, "0", STR_PAD_LEFT);
if(strpos($url, "/") === false)
$url .= "/";
return $url.$id;
}

//在模板中调用函数
{foreach item=article from=$articles}
文章编号:{$article.id}
<img src="{getphotopath id=$article.id, url=$sitephoto}" width="150" height="160" border="0" />
{/foreach}

直接调用 {$article.id} 是正确的,在自定义函数里调用 $article.id 居然是 array.id
该如何调用才对?
...全文
162 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgseamaple 2010-08-13
  • 打赏
  • 举报
回复
非常感谢 dingsongtao
Dleno 2010-08-13
  • 打赏
  • 举报
回复
{getphotopath id=$article.id, url=$sitephoto}

{getphotopath id=$article.id url=$sitephoto}中间用空格,不是逗号。
CunningBoy 2010-08-13
  • 打赏
  • 举报
回复
{foreach item=article from=$articles}
改为
{foreach item="article" from=$articles}

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧