Search code examples
kotlinfreemarkercapitalization

Kotlin Extension Methods in Freemarker Template


Given the Freemarker template

class ${name.capitalize()}

and the data

val data = mapOf("name" to "test")

I'd like to get the following result by applying the extension method capitalize() on name:

class Test

However, an error is thrown:

FreeMarker template error:
For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> name  [in template "table.ftl" at line 1, column 24]

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${name.capitalize()}  [in template "table.ftl" at line 1, column 22]
----

Solution

  • Use Freemarker's capitalize built in:

    class ${name?capitalize}
    

    The string with all words capitalized.