Is it possible to render lists using jinjava library?
So far I could only render HashMaps. I'm using the following function to convert case classes to hashmaps.
def toNestedJavaMap(caseClass: AnyRef): util.Map[String, Any] =
(new java.util.HashMap[String, Any]() /: caseClass.getClass.getDeclaredFields) { (accumulator, field) =>
field.setAccessible(true)
val value = field.get(caseClass) match {
case caseClassInstance: Product => toNestedJavaMap(caseClassInstance)
case x => x
}
accumulator.put(field.getName, value)
accumulator
}
What should I pass as context in order to render things like {% for item in items %}
?
Not sure why I didn't try passing an implementation of the java Iterable interface...
This is the working version modified to take into account the conversion when having Stream alike scala types.
def toNestedJavaMap(caseClass: AnyRef): util.Map[String, Any] =
(new java.util.HashMap[String, Any]() /: caseClass.getClass.getDeclaredFields) { (accumulator, field) =>
field.setAccessible(true)
val value = field.get(caseClass) match {
case listInstance: Stream[AnyRef] => listInstance.map(toNestedJavaMap).toList.asJava
case caseClassInstance: Product => toNestedJavaMap(caseClassInstance)
case x => x
}
accumulator.put(field.getName, value)
accumulator
}