freemarker

Freemarker: call a static util method from a template file (*.ftl)


In a Freemarker FTL file, I want to call StringUtils.capitalize(myString). For example:

<p>You selected ${selectionString}.</p>
<p>${StringUtils.capitalize(selectionString)} is great.</p>

Can I somehow import org.apache.commons.lang3.StringUtils?


Solution

  • You can't #import a class, only other templates.

    Note that you can achieve a similar result with #assign StringUtils=statics['org.apache.commons.lang3.StringUtils'], as far as you add objectWrapper.getStaticModels() as statics to the set of shared variables in the Configuration or add it to the data-model.