Using Scala and StringTemplate, how do I loop through a Map

Posted by Marcus Kazmierczak on Stack Overflow See other posts from Stack Overflow or by Marcus Kazmierczak
Published on 2010-06-11T14:18:24Z Indexed on 2010/06/11 15:03 UTC
Read the original article Hit count: 263

I have my environment setup nicely using Scala, StringTempalte within the Google AppEngine. I am having trouble looping through a Map and getting it to display in the template. When I assign a simple List of just Strings to the template it works using:

In Scala Servlet:

  var photos = List[String]()

  //... get photo url and title ...

  photos = photo_url :: photos
  template.setAttribute("photos", photos: _*)

In Template:

  $photos: { photo|
    <div><img src="$photo$_s.jpg"></div>
  }$

The above works. However, any attempt of creating a Map using url and title and assigning to the template gives me an error. Here is my attempt, which does not work:

In Scala Servlet:

  var photos = List[Map[String,String]]()

  //... get photo url and title ...

  photos = Map("url" -> url, "title" -> title) :: photos
  template.setAttribute("photos", photos: _*)

In Template:

  $photos: { photo|
    <div><img src="$photo.url$_s.jpg" title="$photo.title$"></div>
  }$

This gives me the following error

Class scala.collection.immutable.Map$Map2 has no such attribute: title in template context 

Thoughts / Ideas ?

© Stack Overflow or respective owner

Related posts about google-app-engine

Related posts about scala