Alternating table row colors in freemarker

Posted by itsadok on Stack Overflow See other posts from Stack Overflow or by itsadok
Published on 2009-02-03T07:36:53Z Indexed on 2010/05/19 18:40 UTC
Read the original article Hit count: 547

Filed under:
|

What's a good, simple way to have alternate row coloring with freemarker?

Is this really the best way?

<#assign row=0>
<#list items as item>
    <#if (row % 2) == 0>
        <#assign bgcolor="green">
    <#else>
        <#assign bgcolor="red">
    </#if>
    <tr style='background-color: ${bgcolor}'><td>${item}</td></tr>
    <#assign row = row + 1>
</#list>

I tried doing this:

<#assign row=0>
<#list items as item>
    <tr style='background-color: ${(row % 2) == 0 ? "green" : "blue"}'><td>${item}</td></tr>
    <#assign row = row + 1>
</#list>

But apparently you can't user the ternary operator in there.

Note: I guess I should have mentioned it earlier, but I can't use css classes or javascript, since this HTML is going into an email message.

© Stack Overflow or respective owner

Related posts about freemarker

Related posts about template