groovyjava-8playframework-1.x

How to iterate map in a template using play framework 1.4.2


I have got a map where the key is a object but the value is a list object. I am using play framework 1.4.2 (using groovy template).

This is the map:

Map<Object, List<Object>> map = new HashMap<>();

How can iterating through the map values in a template?


Solution

  • You can use entrySet() to iterate over the map just like you would do in java (see this SO answer), then create a variable in a groovy script to get the value and key :

    #{list items:map.entrySet(), as:'set' }
      ∗{print the key}*
      ${set.key}
      *{iterate over value list }∗
      #{list items: set.value, as:'itemValue'}
        ${itemValue}
      #{/list}
    
    #{/list}
    

    For more information about groovy scripts in play see : Play documentation