Search code examples
playframeworkplayframework-2.0twirl

create List inside twirl template


Usually I create all the stuff like that:

@(courses: List[models.Course])

Then I pass the List from the controller into the view from the render() method.

but this is some kind of special case, It's actually a partial and I would have to add the list of courses into every method in the controllers.

logic:

main.scala.html is the main file which calls all the other views via a @content variable.

There is a twitter bootstrap navbar which get's called into every view, I don't want to pass the List from every controller action into the view but instead I'd like to call it like so:

pseudo code:

@List[Course] = { Course.find.all()) { courses => 
    @for(c <- courses) {
        @c.getCategory()
     }
}

notes: This is pseudo code I have no idea about Scala.


Solution

  • What I did was that:

     @defining( CourseCategory.find.all()) { courses =>
           @for(i <- courses) {
              <li><a href="#">@i.getCategoryName</a></li>
            }
      }
    

    I'm not sure if this is a good approach but it works.