Q:

Playframework scala template variable definition

I have a accordion group in my play scala template, the first accordions body needs be in class "in collapse" (eg : class="accordion-body in collapse" ) and other accordions need to have class="accordion-body in collapse".
How do I define variables in scala template so that I can set the accordions class appropriately based on if it's a first record / not.
@for(t <- tests) {
   <div class="accordion-group">
       <div class="accordion-heading">
   <a class="accordion-toggle" data-toggle="collapse" data-parent="#basic-accordion" href="#Test-@t.getId()">@t.getName()</a>
       </div>
       <div id="collapse-@t.getId()" class="accordion-body in collapse">
          <div class="accordion-inner">
          <p>@t.getDescription()</p>
          <p> The demo will get expired on - @t.getEndDate() </p>
      </div>
    </div>
   </div>
}

A:

You can use @for loop on scala templates like this :

@for((t, index) <- tests.zipWithIndex) {
    @if(index == 0) {
        // first index
        ...
    } else {
        ...
    }
}

The index variable is automatically assigned and and incremented for each loop starting with 0 as the first index.

