szymond
szymond

Reputation: 1310

How do I break out of a play framework template loop?

Given a loop in template like this:

@for(item <- items) {   
    @if(item.id == 42) {    
        BREAK
    }
}   

How can I make it break? Is break/continue construct available to use in play framework template?

Upvotes: 3

Views: 1157

Answers (1)

avik
avik

Reputation: 2708

Assuming items is a Scala collection, the idiomatic approach is not to break, but to filter out the elements you don't want to process before you start iterating.

I'm guessing that your collection is ordered by ID, and your intention is to stop once you get to item 42. If this indeed is the case, I'd go for this:

@for(item <- items.filter(_.id < 42)) {
  // Do stuff
}

Upvotes: 5

Related Questions