Scala Play Framework templating complex objects

73 Views Asked by At

I am looking to render some templates and pass in nested objects. I havent seen many examples of this. It compiles, but Im looking more for ideas on how idiomatic the approach is.

For example, say the following case classes are built from a Slick database query

case class ThemedListOfAlbums(
 themeName: String,
 description: String,
 albums: List[Album]
)
case class Album(
 name: String,
 artist: String,
 imageUrl: String
 songs: List[Song]
)
case class Song(
 name: String,
 imageUrl: String,
 description: String
)

is it then just as simple as passing the complex object?:

@(user: User, catalogs: List[ThemedListOfAlbums])
 //stuff
<ul>
@for(c <- catalogs) {
 //?? want to render a partial for each catalog with a list of album images 
      //with song names on them
} 

Is there a limit to levels of nesting before I need to rethink what I am trying to render completely?

a link to an example would be helpful if this qualifies as a "do your homework" question. cant seem to find any pertinent info

1

There are 1 best solutions below

2
On

Provided your Slick query properly populates ThemedListOfAlbums.albums and Album.songs then I don't see why this wouldn't work

I'd be more interested to see the kinds of queries you're using as this kind of approach can very quickly lead to a large amount of database queries for a single page

For example, one query could easily get a list of Album for the root object, but if each Album then makes a database call for a list of related Songs then you're going to have a huge amount of database requests, especially considering your view contains a list of ThemedListOfAlbums