groupe-sii / ogham

Sending email, sms or whatever is a piece of cake
https://groupe-sii.github.io/ogham/
Apache License 2.0
21 stars 14 forks source link

Add possibility to register utility classes to be used in templates #83

Open aurelien-baudet opened 4 years ago

aurelien-baudet commented 4 years ago

When using Thymeleaf with Spring, we can use ${T(full.package.name.ClassName).staticMethod(args)} to call a static method of an utility class. Without Spring, the syntax seems to be ${@full.package.name.ClassName@staticMethd(args)}. For FreeMarker, only java.lang.System is exposed so you have to register manually class hashes using root.put("statics", BeansWrapper.getDefaultInstance().getStaticModels()); or change the security level to EXPOSE_ALL.

In order to provide a better use experience for developers that don't really need to know how Thymeleaf or Freemarker work, Ogham should handle an abstraction to declare some utility functions with some alias.

For example, user could configure an alias (Dates) to call org.example.util.DateUtil.format(args). In templates you could then use it like ${Dates.format(args)} for all template engines.

FreeMarker allows to register TemplateHashModel so alias would work easily. Need to check how to add this feature with Thymeleaf.