MiLe
MiLe

Reputation: 213

Hashmap in Freemarker Macro is not working

I need to iterate through a hashmap in a freemarker macro. But for some reason it doesnt work and I cannot find my mistake.

This is the macro that I wrote to test the iteration:

<#macro listtest products>

    <#list products as name, price >
        ${name}: ${price} <br>
    </#list>

</#macro>

And this is the example that I wrote:

<@listtest products={ "apple": 5, "banana": 10, "kiwi": 15 } />

I get the following error message regarding the line with "<#list products as name, price >":

Encountered ",", but was expecting: ">"

What am I doing wrong here?

Upvotes: 2

Views: 97

Answers (1)

Ori Marko
Ori Marko

Reputation: 58792

You can use ?keys to iterate over JSON entries:

<#list products?keys as k>
  ${k} : ${products[k]} <br>
</#list>

Upvotes: 2

Related Questions