smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.25k stars 709 forks source link

@iteration variable error in include #877

Closed elvispdosreis closed 1 year ago

elvispdosreis commented 1 year ago

I have a problem with an iteration variable, I need to somehow use it in the main template, if I try to use it in the include template without having used it in the main one, I get the error "Undefined property: Smarty_Variable::$iteration"

does not work

{foreach $produtos as $produto}
  {include file="item.tpl"}
{/foreach}

it works

{foreach $produtos as $produto}
  {if $produto@iteration >= 0}
   {include file="item.tpl"}
  {/if}
{/foreach}

item.tpl

{$produto@iteration}

PHP: 8.1 Smarty: 4.3.1

wisskid commented 1 year ago

Have you tried passing it as a variable? This might work:

{foreach $produtos as $produto}
  {include file="item.tpl" iteration=$produto@iteration}
{/foreach}

And change item.tpl so it used $iteration instead of $produto@iteration.

elvispdosreis commented 1 year ago

in version 3.1 I didn't have this behavior, more in the way you suggested also works

smarty/smarty: 3.1 php 7.4

wisskid commented 1 year ago

Actually, I have the same error "Undefined property: Smarty_Variable::$iteration" with Smarty 3.1.46. Which version are you using?

wisskid commented 1 year ago

Closed because of inactivity.