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.