Is it possible to alternate every other tr with {foreach} Smarty tags?

Discussion in 'Templates customization' started by petrovich, Aug 27, 2010.

  1. petrovich

    petrovich New Member

    Joined:
    Jul 16, 2010
    Messages:
    4
    I would like my tables (ie the Receipt page and Your Payment History tab) to have alternate row classes. Is this possible with the Smarty {foreach} tags? Here's the code as is:

    Code:
    	{foreach from=$receipt_products item=p}
    	<tr>
    		<td>{$p.title}</td>
    		<td>{$config.currency|default:"$" {$p.subtotal|string_format:"%.2f"}</td>
    	</tr>
    	{/foreach}
    
    
    And here's an example of the code that I would like have automatically generated (see how one row is odd, while the other is even):

    Code:
    	<tr class="odd">
    		<td>{$p.title}</td>
    		<td>{$config.currency|default:"$" {$p.subtotal|string_format:"%.2f"}</td>
    	</tr>
    	<tr class="even">
    		<td>{$p.title}</td>
    		<td>{$config.currency|default:"$" {$p.subtotal|string_format:"%.2f"}</td>
    	</tr>
    
    Any creative suggestions on how I could accomplish this?
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Sure
    PHP:
    {foreach from=$receipt_products item=p}
        <
    tr class="{cycle values="even,odd"}">
            <
    td>{$p.title}</td>
            <
    td>{$config.currency|default:"$" {$p.subtotal|string_format:"%.2f"}</td>
        </
    tr>
    {/foreach}
  3. davidm1

    davidm1 aMember User & Partner

    Joined:
    May 16, 2006
    Messages:
    4,437
    cycle values="even,odd"

    You can also use it like

    cycle values="type1,type2,type3"

    Thats cool code :)
    Didnt know about that. Thanks!


    David
  4. petrovich

    petrovich New Member

    Joined:
    Jul 16, 2010
    Messages:
    4
    Great! Thank you very much!

Share This Page