Smarty: foreach, every 2nd iteration

Skinning and designing Serendipity (CSS, HTML, Smarty)
Post Reply
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Smarty: foreach, every 2nd iteration

Post 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
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: Smarty: foreach, every 2nd iteration

Post 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
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Smarty: foreach, every 2nd iteration

Post by Don Chambers »

how about...

Code: Select all

{foreach from=$whatever item="whatever" name=myloop}
    {if $smarty.foreach.myloop.iteration is even}
=Don=
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: Smarty: foreach, every 2nd iteration

Post 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
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: Smarty: foreach, every 2nd iteration

Post by Don Chambers »

Happy to help. The Kinetic template uses this odd/even iteration as well.
=Don=
onli
Regular
Posts: 2829
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Smarty: foreach, every 2nd iteration

Post by onli »

Code: Select all

{if $smarty.foreach.myloop.iteration is even}
Which ist what timbalu suggested ;)
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: Smarty: foreach, every 2nd iteration

Post 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
Post Reply