0

I'm receiving not found: value counter even though the variable is defined. Any help with this? I'm new with scala and everything is new to my eyes. Thanks

@{def counter = 0}
@for(atg <- Activity.groupContiguous(activityGroup)) {
  @if(!atg.isEmpty) {
    @views.html.activity.activityTypeGroup(atg, counter))
  }
  counter = counter + 1
}
serejja
  • 22,901
  • 6
  • 64
  • 72
Monnster
  • 625
  • 7
  • 16

2 Answers2

3

You code doesn't work because @{def counter = 0} doesn't define anything in the template scope and returns Unit. I don't know any convenient way to define mutable variable in the scala template, and this is actually discouraged.

The code could be easily rewritten using functional approach:

@for((atg, counter) <- Activity.groupContiguous(activityGroup).filterNot(_.isEmpty).zipWithIndex) {
  @views.html.activity.activityTypeGroup(atg, counter))
}
vitalii
  • 3,335
  • 14
  • 18
  • Thanks vitali, additional question. How can I increment counter value? I tried counter = counter + 1 but upon checking in the view the result became 0 = counter + 1; – Monnster Feb 07 '14 at 12:42
0

Take a look to other similar issue, you can solve it with zipWithIndex https://stackoverflow.com/a/14613877/1066240

Yo'll find there also other useful examples.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182