Sunday, March 23, 2014

Scala Play Framework Template Imports

The template compiler is pretty sweet. Using a template like a function is awesomely simple.  If you have a bit of html code that makes up a reused block on more than one page you just factor it out to it's own ___.scala.html. Then reference it with the "magical" @ character.

However, the error messages that the template engine provides are less-than-detailed. I had created a subdirectory named: app/views/tags. When I tried to use one of the template functions out of that directory I got the error: not found: value gallery

So let's take a look at the important parts.

I have templates:

  • app/views/main.scala.html
  • app/views/tags/gallery.scala.html
With this as the important parts of main.scala.html

@(artist: models.ArtistModel,
      tags: List[String])(implicit artistModel: Option[models.ArtistModel]) @import tags._
.....
...
@gallery()....
Hmmmm, "not found: value gallery"? Why can't it find gallery? I imported it. It's formed correctly. Well, this is painfully obvious now, but it took a few minutes for me to reconcile that the "tags: List[String]" is clashing with the "@import tags._" 

More on the Play Framework Templates