Nick Manning Nick Manning - 1 year ago 124
PHP Question

How to access smarty's foreach iteration from php function? (Smarty 2.6)


Say I have

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

Then down below I call:

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

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
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 PHP or Javascript allowed.


Answer Source

scope=global is required for access variable from foreach!

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


{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}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download