2

I have the following smarty code:

  {foreach from=$additional_fields item=v}
    {if $v.fieldid eq 5}
       {php}
         // several pounds of php code here
         $myfieldid = {$v.fieldid}; // this is wrong
       {/php}
    {/if}
  {/foreach}

I'm trying to grab the current field id with my custom php code, in other words {$v.fieldid}. I've found a few posts referencing $this->_tpl_vars[somevar] to get smarty variables when inside the {php} tags, but that doesn't seem to work with foreach.

I realize that using the {php} tags in smarty is counter-intuitive to the whole smarty concept and like totally lame, but I have my reasons. Thanks for the help!

3 Answers 3

2

change $myfieldid = {$v.fieldid}

to

$myfieldid = $v['fieldid'];

by the way what you are doing is evil!

Sign up to request clarification or add additional context in comments.

1 Comment

Pure evil! Bingy, that didn't work inside the php tags. In your example, php is looking for an array named $v, but $v is a smarty variable, so it doesn't exist in the php world I suppose. Thanks for the suggestion though.
1

Although this is kind of old topics, I managed to solve it combining a bit of suggestion from Bingy.

First you get the smarty variable into php by using get_template_vars then take the array value.

$v = $this->get_template_vars('v'); 
$myfieldid = $v['fieldid'];

OR:

$myfieldid = $this->_tpl_vars['v']['fieldid'];

Comments

0

If you are using PHP already in a smarty template, why don't you implement foreach as PHP loop and not smarty loop?

1 Comment

There is a bunch of smarty code inside that foreach. I simplified the code for this question. @Bingy - will try your tip and get back to you later today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.