简体   繁体   English

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

[英]How to access smarty's foreach iteration from php function? (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? 我如何从函数内部访问此foreach的迭代,最好不传递参数?

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. 由于我知道您会告诉我{php}已过时……我使用它的原因是因为系统要求我制作一个非常复杂的模板,因此我需要复杂的函数来构建它,而不会使其变得一团糟,而且我以前从没用过聪明人。 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. 我唯一会知道如何管理此代码的方法是使用常规PHP,但老板坚持要用smarty来完成整个工作……不允许使用PHP或Javascript。

Thanks! 谢谢!

This is horrible style of coding, it's not a solution to use such code because you are in hurry. 这是一种可怕的编码风格,这不是解决方案,因为您很着急。 And if you don't know at all some technology, you shouldn't take money for this. 而且,如果您根本不了解某些技术,则不应为此花钱。

You could use in Smarty: 您可以在Smarty中使用:

{foreach from=$x item=panelObj name=panellist}
    {assign var="iteration" value=$smarty.foreach.panellist.iteration}
    {php}hello(){/php}
{/foreach}

And in PHP function: 并在PHP函数中:

function hello()
{
    global $smarty;
    ?>
    <div class="hello"><?php echo $smarty->get_template_vars('iteration'); ?>
    </div>
<?php
}

and it will work (I've tested it) but I wouldn't even try to use such code for any project 并且它可以工作(我已经测试过),但是我什至不会尝试在任何项目中使用这样的代码

scope=global is required for access variable from foreach! scope = global是来自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} {assign var =“ edit_ticket_id” value = $ ticket.ticketRaw ['edit_old_ticket_id'] scope = global}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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