Page 1 of 1

[SOLVED] - [{foreach}] - color every other entry?

Posted: Wed Jul 18, 2007 1:49 pm
by rtkd
heya,

i want to color every other div in a foreach loop.

Code: Select all

{foreach from=$array item=xxx}
	{if ???}
		<div class="1"></div>
	{else}
		<div class="2"></div>
	{/if}
{/foreach}
is there an easy way to archive this?
i read something about "iteration" but don't know how to use it.
right now i'm thinking i'll have to do it with php.

rtkd

Re: [{foreach}] - color every other entry?

Posted: Wed Jul 18, 2007 2:11 pm
by calguy1000
read the smarty manual at http://smarty.php.net  there's a function there to do this easily.
No php coding required.

Re: [{foreach}] - color every other entry?

Posted: Wed Jul 18, 2007 2:14 pm
by rtkd
jup. i did already and stumbles upon this

{if $smarty.foreach.foo.iteration % 2 == 1}

but strangely it's not working?

Code: Select all

{foreach from=$array item=xxx name=foo}
	{if $smarty.foreach.foo.iteration % 2 == 1}
		<div style="background-color:#ffff00;">test</div>
	{else}
		<div style="background-color:#ff0000;">test</div>
	{/if}
{/foreach}
only renders yellow entries?

Re: [{foreach}] - color every other entry?

Posted: Wed Jul 18, 2007 2:16 pm
by calguy1000
see {cycle} in the smarty manual.

Re: [{foreach}] - color every other entry?

Posted: Wed Jul 18, 2007 2:25 pm
by rtkd
ok, tried that, but then all the other ALBUM vars are gone...?

Code: Select all

{foreach from=$albums item=album name=foo}

Re: [{foreach}] - color every other entry?

Posted: Wed Jul 18, 2007 2:30 pm
by calguy1000
Try something like:

Code: Select all

{foreach from=$array item=xxx}
  <div style="background-color: {cycle values="#ffff00,#ff0000"}"; >
      ...
  </div>
{/foreach}
or

Code: Select all

{foreach from=$array item=xxx}
   <div class="{cycle values="row1,row2"}">
      ...
   </div>
{/foreach}

Re: [{foreach}] - color every other entry?

Posted: Wed Jul 18, 2007 2:37 pm
by rtkd
ahhh :)

stupid me, i thought cycle would only work in combination with section like in the
example on smarty.net.

now it works like a charm!

thx and have a nice day!