Page 1 of 1

Cycling emitted html in entries.tpl

Posted: Mon Dec 15, 2008 2:00 pm
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

Re: Cycling emitted html in entries.tpl

Posted: Mon Dec 15, 2008 2:13 pm
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

Re: Cycling emitted html in entries.tpl

Posted: Mon Dec 15, 2008 2:15 pm
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

Posted: Mon Dec 15, 2008 2:35 pm
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

Posted: Mon Dec 15, 2008 2:37 pm
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}

Posted: Mon Dec 15, 2008 4:34 pm
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.

Posted: Mon Dec 15, 2008 5:11 pm
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?

Posted: Mon Dec 15, 2008 7:28 pm
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.

Posted: Mon Dec 15, 2008 7:38 pm
by Don Chambers
I have not tried it, but that does look like it would have done the trick.

Posted: Mon Dec 15, 2008 9:52 pm
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

Posted: Tue Dec 16, 2008 10:51 am
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

Posted: Tue Dec 16, 2008 10:54 am
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