简体   繁体   中英

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, ie to upperCase the key (which is a string):

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

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: http://freemarker.org/docs/ref_builtins.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM