Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternating table row colors in freemarker

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.

like image 648
itsadok Avatar asked Feb 03 '09 07:02

itsadok


1 Answers

class='${["odd", "even"][item_index%2]}'
like image 167
Dinesh Khetarpal Avatar answered Jan 05 '23 00:01

Dinesh Khetarpal