Freemarker Hashtable <Integer, String>, Key Iteration
I have a number of hash tables with integers as keys, and I want to be able to iterate over them in my Freemarker templates, however, nothing works.
I tried an example from Freemarker iterating over hash key keys :
<#list user.props() as prop> ${prop} = ${user.get(prop)} </#list> It probably works with strings as keys, but not with integers. I also cannot get the value from my hash table for a specific value. I have:
Hashtalbe ht = new Hashtable(); ht.put(1, "hello"); datamodel.put("devices", ht); (datamodel is 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 this works. Can you help me please?
PS. I converted the hash table from to to pass it to the template, but this seems like a workaround.
Regards, Timofey
for those of you who can follow my footsteps. Apparently FreeMarker cannot work with Hashtables with options. So I ended up creating versions of these inti hash tables, and since I had numbers as keys in my hash tables, 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 strength can be with you :)
This is an old problem that the FTL hash type is not like Java Map , but only supports string keys . But with 2.3.22 you can use someMap?api.get(someNonStringKey) to work. It needs some configuration to enable, but nothing violates the existing application. See this answer or this FAQ item .