Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load FreeMarker templates from database

I would like to store my FreeMarker templates in a database table that looks something like:

template_name | template_content --------------------------------- hello         |Hello ${user} goodbye       |So long ${user} 

When a request is received for a template with a particular name, this should cause a query to be executed, which loads the relevant template content. This template content, together with the data model (the value of the 'user' variable in the examples above), should then be passed to FreeMarker.

However, the FreeMarker API seems to assume that each template name corresponds to a file of the same name within a particular directory of the filesystem. Is there any way I can easily have my templates loaded from the DB instead of the filesystem?

EDIT: I should have mentioned that I would like to be able to add templates to the database while the application is running, so I can't simply load all templates at startup into a new StringTemplateLoader (as suggested below).

like image 562
Dónal Avatar asked Dec 10 '08 19:12

Dónal


People also ask

How do I use FreeMarker template in spring boot?

Freemarker Template FilesSpring boot looks for the The template files under classpath:/templates/. And for the file extension, you should name the files with . ftlh suffix since Spring Boot 2.2. If you plan on changing the location or the file extension, you should use the following configurations.

What is a template loader?

Template loaders are objects that load raw textual data based on abstract template paths like "index. ftl" or "products/catalog. ftl" . It's up to the concrete template loader if from where and how the template "files" are loaded.

What is a FreeMarker template?

FreeMarker is a Java-based template engine which can be used in stand-alone or servlet-based Java programs. In FreeMarker you define templates, which are text files that contain the desired output, except that they contain placeholders like ${name} , and even some logic like conditionals, loops, etc.

What is a .FTL file?

An FTL file is a template used by FreeMarker, a Java template engine used to auto-generate text output. It contains source text as well as FreeMarker variable definitions and instructions that are used as placeholders for text substitutions. FTL files are commonly used for auto-generating HTML webpages, .


2 Answers

We use a StringTemplateLoader to load our tempates which we got from the db (as Dan Vinton suggested)

Here is an example:

StringTemplateLoader stringLoader = new StringTemplateLoader(); String firstTemplate = "firstTemplate"; stringLoader.putTemplate(firstTemplate, freemarkerTemplate); // It's possible to add more than one template (they might include each other) // String secondTemplate = "<#include \"greetTemplate\"><@greet/> World!"; // stringLoader.putTemplate("greetTemplate", secondTemplate); Configuration cfg = new Configuration(); cfg.setTemplateLoader(stringLoader); Template template = cfg.getTemplate(firstTemplate); 

Edit You don't have to load all templates at startup. Whenever we will access the template, we'll fetch it from the DB and load it through the StringLoader and by calling template.process() we generate (in our case) the XML output.

like image 177
Ulf Lindback Avatar answered Sep 28 '22 00:09

Ulf Lindback


A couple of ways:

  • Create a new implementation of TemplateLoader to load templates direct from the database, and pass it to your Configuration instance using setTemplateLoader() prior to loading any templates.

  • Use a StringTemplateLoader that you configure from your database when your application starts. Add it to the configuration as above.

Edit in light of the questioner's edit, your own implementation of TemplateLoader looks like the way to go. Check the Javadoc here, it's a simple little interface with only four methods, and its behaviour is well documented.

like image 37
Dan Vinton Avatar answered Sep 28 '22 01:09

Dan Vinton