Freemarker row counter

mathisfenne
mathisfenne used Ask the Experts™
on
I am creating a html template in freemarker. If number of rows count 1, I want to use a div with the class "class1". If number of rows counts more than one, I want to use a different class "class2".
I dont know how to do it. Any ideas?



<#list viewer.data as row>
  <div class="class1">
    ${row.fields.title.text}
    ${row.fields.introtext.text}
  </div>
</#list>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jim CakalicSenior Engineer

Commented:
I'm not sure I understand your question. Here are some possibilities

1) If the collection contains one item assign class1 else assign class2:
<#list viewer.data as row>
  <#if viewer.data?size == 1>
 
  <#else>
 
 
    ${row.fields.title.text}
    ${row.fields.introtext.text}
 


2) Assign the first item class1 and all others class2:
<#list viewer.data as row>
  <#if row_index == 1>
 
  <#else>
 
 
    ${row.fields.title.text}
    ${row.fields.introtext.text}
 


3) Alternate assignment of classes (1st item is class1, 2nd item is class 2, 3rd item is class1, etc.):
<#list viewer.data as row>
  <#if (row_index + 1) % 2 == 0>
 
  <#else>
 
 
    ${row.fields.title.text}
    ${row.fields.introtext.text}
 


Regards,
Jim
Jim CakalicSenior Engineer

Commented:
If the collection contains one item assign class1 else assign class2:

<#list viewer.data as row>
  <#if viewer.data?size == 1>
  <div class="class1">
  <#else>
  <div class="class2">
  </#if>
    ${row.fields.title.text}
    ${row.fields.introtext.text}
  </div>
</#list>

Open in new window

Jim CakalicSenior Engineer

Commented:
Assign the first item class1 and all others class2:
<#list viewer.data as row>
  <#if row_index == 1>
  <div class="class1">
  <#else>
  <div class="class2">
  </#if>
    ${row.fields.title.text}
    ${row.fields.introtext.text}
  </div>
</#list>

Open in new window

Senior Engineer
Commented:
Alternate assignment of classes (1st item is class1, 2nd item is class 2, 3rd item is class1, etc.):
<#list viewer.data as row>
  <#if (row_index + 1) % 2 == 1>
  <div class="class1">
  <#else>
  <div class="class2">
  </#if>
    ${row.fields.title.text}
    ${row.fields.introtext.text}
  </div>
</#list>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial