Search code examples
spring-bootfreemarker

How can I auto include whole Freemarker template package in Spring Boot?


I have layout.ftl file auto include configured in application.properties like this:

spring.freemarker.settings.auto_include = macro/layout.ftl

That works fine, however now I'm adding more and more macros in the macro package, so my configuration is slowly getting flooded like this:

spring.freemarker.settings.auto_include = macro/layout.ftl, macro/macro1.ftl, macro/macro2.ftl, macro/macro3.ftl, macro/macro4.ftl

Is it possible to auto include whole macro package at once? I tried:

spring.freemarker.settings.auto_include = macro
spring.freemarker.settings.auto_include = macro/
spring.freemarker.settings.auto_include = macro/*
spring.freemarker.settings.auto_include = macro/*.ftl

None of these seem to work.


Solution

  • Not out of the box, because the TemplateLoader interface (which is what Spring and others implement to load the templates from somewhere) has no directory listing functionality, so core FreeMarker functionality can't do that.

    If you can configure FreeMarker from Java code, then you can assemble the list of file names dynamically, and then give it to Configuration.setAutoIncludes(List templateNames). At least with some Spring Boot components it's possible to hook in your own setup code after the properties from the application.properties were applied, but I haven't checked if that's so with the FreeMarker Spring Boot feature too.