Page 1 of 1

Smarty: foreach, every 2nd iteration

Posted: Wed Jan 26, 2011 4:18 pm
by yellowled
Guys,

I'm racking my brains here, searching my ass off, but I can't find it. Is there any way in smarty to say something like "on every alternate iteration of a foreach loop, emit this piece of code"? ("alternate" meaning every 2nd, 4th, 6th etc.)

TIA, YL

Re: Smarty: foreach, every 2nd iteration

Posted: Wed Jan 26, 2011 4:24 pm
by Timbalu

Code: Select all

{foreach $myVar as $var}
  {if $var@iteration is div by 2}
    <b>{$var}</b>
  {/if}
  {$var}
{/foreach}
how about something like this?

HTH,
Ian

Re: Smarty: foreach, every 2nd iteration

Posted: Wed Jan 26, 2011 4:37 pm
by Don Chambers
how about...

Code: Select all

{foreach from=$whatever item="whatever" name=myloop}
    {if $smarty.foreach.myloop.iteration is even}

Re: Smarty: foreach, every 2nd iteration

Posted: Wed Jan 26, 2011 4:46 pm
by yellowled
Don Chambers wrote:how about...

Code: Select all

{foreach from=$whatever item="whatever" name=myloop}
    {if $smarty.foreach.myloop.iteration is even}
My hero! :) (Actually, I need "is odd", but it works.)

Thanks.

YL

Re: Smarty: foreach, every 2nd iteration

Posted: Wed Jan 26, 2011 4:52 pm
by Don Chambers
Happy to help. The Kinetic template uses this odd/even iteration as well.

Re: Smarty: foreach, every 2nd iteration

Posted: Wed Jan 26, 2011 4:59 pm
by onli

Code: Select all

{if $smarty.foreach.myloop.iteration is even}
Which ist what timbalu suggested ;)

Re: Smarty: foreach, every 2nd iteration

Posted: Wed Jan 26, 2011 6:48 pm
by yellowled
onli wrote:

Code: Select all

{if $smarty.foreach.myloop.iteration is even}
Which ist what timbalu suggested ;)
That may very well be, but wasn't obvious to my non-coding mind. :)

Fun fact: Didn't need it after all since it was supposed to add extra markup necessary for IE7 only -- client: "IE7? Drop it. Our stats show 70% Firefox and a lot of Safari. We don't care for IE7." :roll:

YL