Creating zebra tables (cycling values)

Smarty provides an excellent function to cycle values inside any loop: cycle. Here is an example how to create a zebra table of article headlines using this function:

{init_articles name="articles"}

<table>
    {foreach from=$articles item="article"}
    <tr style="background-color: {cycle values="gray,white"};">
        <td><a href="{$article->href}">{$article->title}</a></td>
    </tr>
    {/foreach}
</table>

Each call to the cycle function prints out the next value in the values list so every other row in that sample has gray background. In real-life solution this would use CSS classes for defining row colors, for example: <tr class="{cycle values="light,dark"}">. And of course there can be more than two values which to cycle and the values can be variables.

Site development

About Saurus API / Creating page template tutorial / Creating content template tutorial / Applying custom design to page template checklist / Moderated comments / Stationary articles (header, footer etc) / First, last and n'th item in a foreach loop / Creating zebra tables (cycling values) / Site and Article editor CSS styles / How to set up website aliases / Customising v-shaped editor buttons / Using PHP in templates / Exchanging variables between templates and PHP / Profiles and Custom Assets / Site languages / Printer friendly pages / Enabling browser cache / Creating WAP pages / Configuring sitemap / Saurus 4.1 technical notes