How do I create a sorted map or tree map in velocity templates using liferay?
I am creating a velocity template in liferay portal. I use a HashMap
to store some key/value pairs.
I am creating a HashMap
like this
#set ($myHashMap = {
'key1': 'value1;',
'key2': 'value2;',
'key3': 'value3;',
})
and I access the hashmap using
$myHashMap.get('key1')
I want to iterate over my hashmap and print all the key/value pairs.
But since the ordering is not maintained in HashMap
, I want to use a TreeMap/SortedMap. I am new to velocity templates. Also, I know how to do it in java, but unfortunately the glitch is I have to find a work around in velocity templates.
AFAIK you can't create it directly in the template. The closest thing would be to create a Map
and a ArrayList
of keys and iterate the latter while taking values from the former.
Of course another solution is to pass a TreeMap
/SortedMap
created in java to the template and iterate it's keySet