Using a smarty foreach variable within php tags
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!
change $myfieldid = {$v.fieldid}
to
$myfieldid = $v['fieldid'];
by the way what you are doing is evil!
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'];
If you are using PHP already in a smarty template, why don't you implement foreach as PHP loop and not smarty loop?
精彩评论