Cycling emitted html in entries.tpl

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:

Cycling emitted html in entries.tpl

Post by yellowled »

I'm racking my brains since I seem to remember doing this before, but I can't for the life of me remember how.

I want to emit different code on even and uneven iterations of a {foreach}, i.e. generate something like this:

Code: Select all

<div class="even">...</div>
<div class="uneven">...</div>

<div class="uneven">...</div>
<div class=="even">...</div>
{cycle} would work if I only needed to change the classes, but actually I need to change the content of those divs also. So in "symbolic smarty", I'd need something like

Code: Select all

{if this.is.an.uneven.iteration.of.the.foreach.loop}
...
{else}
...
{/if}
How do I do this? Is it possible at all?

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Cycling emitted html in entries.tpl

Post by garvinhicking »

Hi!

Usually this is done via

Code: Select all

{foreach from=$var name="eyeforaneye"}
{if smarty.foreach.eyeforaneye.iteration % 2 < 1}
....uneven...
{else}
....even...
{/if}
% is the "modulo" operator and returns 0 if a number is evenly dividably by 2, and 1 if there's a rest to the division.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Re: Cycling emitted html in entries.tpl

Post by yellowled »

garvinhicking wrote:Usually this is done via
Well, I have definitely never used that before! :-)

(But I'll give it a try, and I'm sure it will work, so thanks!)

YL
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

Hmmm.

Code: Select all

Fatal error: Smarty error: [in file:/var/www/serendipity/templates/xxx/entries.tpl line 6]: syntax error: (secure mode) 'smarty' not allowed in if statement (Smarty_Compiler.class.php, line 1408) in /var/www/serendipity/bundled-libs/Smarty/libs/Smarty.class.php on line 1092
Typo?

Edit: Found it, there's a $ missing in front of smarty :)

YL
Last edited by yellowled on Mon Dec 15, 2008 2:38 pm, edited 1 time in total.
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

You can also try

Code: Select all

{if $smarty.foreach.eyeforaneye.iteration is even}
    I am an even numbered iteration
{else}
    I am an odd numbered iteration
{/if}
Last edited by Don Chambers on Mon Dec 15, 2008 5:07 pm, edited 1 time in total.
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

Ick. The {cycle} tag is meant for exactly this, after all.

Code: Select all

{foreach from=$var name="eyeforaneye"}
<div class="{cycle values="even,uneven"}">...</div>
{/foreach}
No math required.
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

He didn't want just the class of the div to change for each iteration, he needed the actual div content to change too.

Ultimately, he used a counter since the odd/even concept needed to apply to all iterations within nested foreach loops.... ie

Code: Select all

{foreach loop 1}
    {foreach loop 2}
        need this to preserve odd/even status even if outer loop resets.
Would there have been a better option?
=Don=
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

{cycle} should work inside nested loops. It just skips to the next value every time it's hit. If you want to reset it for each iteration of the outer loop, just assign it a name and use {cycle name="whatever" reset="true"} before you hit the inner loop.

The complex values are something of a problem, though. {cycle} can use an array for its input values, but where are you going to build the array? I thought from the discussion he just wanted even/uneven rows.

You could still avoid the math with:

Code: Select all

{foreach loop 1}
  {foreach loop 2}
    {cycle assign="even" values="true,false"}
    {if $even}
      ... even row ...
    {else}
      ... odd row ...
    {/if}
  {/foreach}
{/foreach}
And I suppose that rows that are *nearly* the same, with only two or three variant portions, could use set the cycle's "advance" attribute to false to avoid premature incrementation. ;) But that seems like a bunch of trouble.
Judebert
---
Website | Wishlist | PayPal
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Post by Don Chambers »

I have not tried it, but that does look like it would have done the trick.
=Don=
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

judebert wrote:You could still avoid the math with:
I suppose it would work, but haven't tested it.

What exactly would be the benefit over Garvins solution? Just plain "coder's ick factor" or does this one maybe save some of Grandma's precious performance pennies?

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

While judeberts solution only uses Smarty-Syntax and doesn't look obfuscated, the solution with less performance implications is the MODULO one (mine). The {cycle} solution first needs to call a function, parse data to the template, and then also evaluate an IF-Check.

The impact would surely be neglectibile (maybe 0.001% of your overall rendering time), so you can choose whatever your eyes prefer :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

garvinhicking wrote:The impact would surely be neglectibile (maybe 0.001% of your overall rendering time), so you can choose whatever your eyes prefer :)
Okay, so I choose not what my eyes prefer but what has already been uploaded for the client :-)

YL
Post Reply