Gerald Mücke Gerald Mücke - 4 months ago 50
Java Question

Freemarker converting a hash to String

To iterate over hashmaps one can use the hash built-in to access values and keys of a Map. Now I want to operate on the key, i.e. to upperCase the key (which is a string):

<#assign keys = formats?keys>
<#list keys as key>
${key.toUpperCase()} = ${formats[key].getFileName()}

But I get the error

For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> key [in template "src/main/resources/test.ftl" at line 41, column 69]

FTL stack trace ("~" means nesting-related):
- Failed at: ${key.toString().toUpperCase()} [in template "src/main/resources/test.ftl" at line 41, column 67]

Any hint how to get the key (which is lowercase) to upper case in the template? or how to convert the hash to a String?


In FreeMarker the Java API of many basic classes (such as String, Number, List, etc.) is hidden. Instead of their API-s, you are supposed to use so called "built-ins", like in this case: key?upper_case. (See also: