doujie9882 2014-10-20 17:01
浏览 58
已采纳

如何从php函数访问smarty的foreach迭代? (Smarty 2.6)

Question

Say I have

{php}
    function hello(){
{/php}
    <div class="hello">{$smarty.foreach.panellist.iteration}</div>
{php}
    }
{/php}

Then down below I call:

{foreach from=$channelObj->get_panellist_primary('','','pan_ptyid,pan_label1,pan_label2') item=panelObj name=panellist}
        {php}hello(){/php}
{/foreach}

I'm getting this error:

PHP Fatal error: Using $this when not in object context in /var/www.app1/theURL/otherthings/channel.tpl.php on line 197

Why doesn't this work? How can I access the iteration of this foreach from inside the function, preferably without passing a parameter?

More Details

Since I know you'll tell me {php} is deprecated...the reason I am using it is because I'm being asked to make a really complicated template, so I need complicated functions to build it without it becoming a mess, and I've never used smarty before. I'm on a deadline and I'm already late, so I can't learn the correct methodology of smarty. The only way I would know how to manage this code is with regular PHP, but my boss is insisting I make the whole thing with smarty...no PHP or Javascript allowed.

Thanks!

  • 写回答

2条回答 默认 最新

  • doujianqin5172 2016-10-20 11:21
    关注

    scope=global is required for access variable from foreach!

    {assign var=SenderID value=$Nachricht.sender_id scope=global} 
    {php} echo $smarty->getTemplateVars('SenderID'); {/php} 
    

    or

    {assign var="edit_ticket_id" value=$ticket.ticketRaw['edit_old_ticket_id'] scope=global}

    {php} global $smarty; var_dump($smarty->get_template_vars('edit_ticket_id')); {/php}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?