How to use Map/List in playframework template
To be even more specific, I wish to access the Map as I iterate over a List of objects, e.g.,
#{list items:itemList, as:'item'} // access the map value using the ${item?.id} as the key #{/list}
but how to use Map like HashMap, TreeMap to play framework template?
This is a generic solution to iterate on Map in using Play! Framework:
in the controller :
render(map);
in the template:
#{list items:map.keySet(), as:'key'} ${map.get(key)} #{/list}
The fact that you want to rely on a side List to iterate on your Map suggest me that you are searching a predictible way for your iteration process.
In that case, just remember that iteration will be unpredictable if you don’t use an ordered/sorted _Map_implementation.
- HashMap gives you an unsorted , unordered Map
- LinkedHashMap maintains insertion order
- TreeMap is the only JDK implementation of a sorted Map. By default it allows you to iterate following the natural order of the elements. You can also specify a custom sort order and implements the Comparable interface. This will lead you to override the compareTo() method.