I have a number of hashtables with Integers as keys, and i want to be able to iterate over them in my Freemarker templates, however, nothing seems to work.
I tried the example from Freemarker iterating over hashmap keys:
<#list user.props() as prop>
${prop} = ${user.get(prop)}
</#list>
It probably works with Strings as keys, but it didn't with Integers. I also can't even retrieve the value from my hashtable by the concrete value. What i have is:
Hashtalbe ht = new Hashtable();
ht.put(1, "hello");
datamodel.put("devices", ht);
(datamodel being the hashmap passed to the template).
In the template i do the following:
<#if devices??>
<#list devices?keys as prop>
<p>${prop}</p>
<p>${devices.get(1)}</p>
OR
<p>${devices.get(key)}</p>
OR
<p>${devices[key]}</p>
OR
<p>${devices[1]}</p>
</#list>
<#else>
<p> no devices</p>
</#if>
But none of that works. Can you help me, please?
PS. I converted the hashtable from into to pass it to the template, but that seems like a bit of a workaround.
Best regards, Timofey
for those of you who may follow my footsteps. Apparently, FreeMarker can't work with Hashtables with as parameters. So I ended up creating the versions of these hashtables inti and, since i had numbers as keys in my hashtables, i was able to do the following in my template:
<#list 1..100 as prop>
<#if hashtable[prop?string]??>
<option value='${prop}'<#if prop==selected> selected='selected'</#if>>${hashtable[prop?string]}</option>
<#else><#break>
</#if>
</#list>
Good Luck and may the force be with you :)
This is the old issue that FTL's hash type isn't like Java's Map
, and it only supports string keys. But, since 2.3.22 you can use someMap?api.get(someNonStringKey)
to work that around. It needs some configuring to enable, but nothing that breaks an existing application. See this answer or this FAQ entry.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With