Handlebars #each in a Play Scala View

If you’re using Play Scala views with Handlebars in them, and you get errors that look like these:

not found: value key
In /home/phil/myproject/app/views/myview.scala.html at line 108.

or

not found: value index
In /home/phil/myproject/app/views/myview.scala.html at line 108.

It’s probably because you’re using {{@index}} when iterating through an array with {{#each myarray}} or {{@key}} when iterating through an object’s properties.

This happens because Play framework views are Scala, and the @ character is a special character. Scala thinks you’re trying to access a Scala value (and comes back with the error above). You’ll need to escape it with @@ so Scala will pass it along as just a single @ for Handlebars to see. It looks like this:

{{#each objects}}
  {{@key}}:{{this}}
{{/each}}

One Comment

  • Matt B.
    Posted December 14, 2013 at 2:38 am | Permalink

    Thanks! This really helped me! Just found what I’ve been looking for.

Post a Comment

Your email is kept private. Required fields are marked *