Go to content

Converting a HashMap into a nested listing in Freemarker

Published on

For the e-Alert we use Freemarker as templating engine. One of the new features of the next release will be a list of all subscribed categories and interests for the recipient.

All subscribed categories and interests from the database are inside a HashMap called userInterests. The names of the categories are the keys, a List of the names of the interests are the values. In the Freemarker template it looks like:

<ul>
[#list userInterests.keySet() as category]
<li>${category}
<ul>[#list userInterests[category] as interest]
<li>${interest}</li>
[/#list]</ul>
</li>
[/#list]
</ul>

Freemarker converts this into the nested list of categories and interests. Eventually it will look like:

  • Information type
    • News
  • Subject
    • Content management
    • Open source