How do I get the second Item in an Iteration?

  I get the first item like this:

{foreach $sArticle.sBlockPrices as $blockPrice}
  {if $blockPrice@first}
    First element
  {else}
    ...
  {/if}
{/foreach}

How do I get the second/third item? 

You are looking for „iteration“: http://www.smarty.net/docsv2/en/language.function.foreach.tpl

Best regards

 Thank you for your answer, I have tried the following, but got an error message:

 

{foreach name=$sArticle.sBlockPrices item=$blockPrice name=blockPrices}
  {if $smarty.foreach.blockPrices.index == 2}
    ...
  {/if}
{/foreach}

 

Should be working like that. But that will give you the third enrty since index starts at 0. iteration starts at one. you mixed those two up.

What does the errormessage say?

Perhaps it’s this: you dont need a $ sign in the item parameter. Just item=blockPrice and than it’s available as $blockPrice in the foreach.

1 Like

Please follow the link: http://www.smarty.net/docsv2/en/language.function.foreach.tpl
There are many examples for index and iteration.

Kind regards

Hey @alex220x‍,

Smarty 3 provides you with additional information inside the foreach loop, therefore you can use the @index modifier to get the index of the current item:

{foreach $sArticle.sBlockPrices as $blockPrice}
  {if $blockPrice@index == 2}
    Second element
  {else}
    ...
  {/if}
{/foreach}

Best regards,
Stephan Pohl  Shopware

1 Like