3

Can mustache loop over a java Map? My object:

Map<Integer, String> mapTest

Tried this in my mustache template:

{{#mapTest}}
  {{.}}
{{/mapTest}}

That spits out a string representation (mapTest.toString()) of the entire object but doesn't loop at all.


Update: In hunting through the source I'm getting closer but still not sure how to do it. https://github.com/scalate/scalate/blob/ec981338c067fcf37106fb5f3bdf2fa8c4e458ca/scalate-core/src/main/scala/org/fusesource/scalate/mustache/Scope.scala#L124-125

James Strachan
  • 9,168
  • 34
  • 31
devth
  • 2,738
  • 4
  • 31
  • 51

2 Answers2

1

See this answer on the mailing list...

http://groups.google.com/group/scalate/msg/100df004001d84f0

James Strachan
  • 9,168
  • 34
  • 31
  • 5
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Joachim Sauer Oct 18 '13 at 14:34
  • Didn't expect a link-only answer from such known expert – hc_dev Feb 16 '21 at 06:55
1

You could convert the Java Map to a Scala Map.

import scala.collection.JavaConverters._
val myScalaMap = myJavaMap.asScala.mapValues(_.asScala.toSet)

I wouldn't hack the template to accommodate a Java object.

Powers
  • 18,150
  • 10
  • 103
  • 108