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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
rtkd
Forum Members
Forum Members
Posts: 126
Joined: Tue Dec 12, 2006 3:57 pm

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

Post 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
Last edited by rtkd on Wed Jul 18, 2007 2:37 pm, edited 1 time in total.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

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

Post by calguy1000 »

read the smarty manual at http://smarty.php.net  there's a function there to do this easily.
No php coding required.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
rtkd
Forum Members
Forum Members
Posts: 126
Joined: Tue Dec 12, 2006 3:57 pm

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

Post 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?
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

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

Post by calguy1000 »

see {cycle} in the smarty manual.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
rtkd
Forum Members
Forum Members
Posts: 126
Joined: Tue Dec 12, 2006 3:57 pm

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

Post by rtkd »

ok, tried that, but then all the other ALBUM vars are gone...?

Code: Select all

{foreach from=$albums item=album name=foo}
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

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

Post 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}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
rtkd
Forum Members
Forum Members
Posts: 126
Joined: Tue Dec 12, 2006 3:57 pm

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

Post 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!
Last edited by rtkd on Wed Jul 18, 2007 3:12 pm, edited 1 time in total.
Post Reply

Return to “CMSMS Core”