Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading freemarker templates folder inside .jar

I am using freemarker templates in my application

Before deploying my application to a jar file, all I needed to do in order to load my templates was this: cfg.setDirectoryForTemplateLoading(new File("templates"));

Which loaded all templates from the template folder I created inside my project.

Now, after moving to maven and deploying my application to an executable jar. The application cannot find this folder anymore (I have checked inside the .jar file and the "templates" folder is deployed right in the root directory)

I have tried everything I know, but with no luck.

How exactly am I supposed to load all my templates now? (I assume that if I put the folder outside the .jar file but in the same directory it will work. but that's not what i want.)

Thanks.

like image 928
Gleeb Avatar asked Feb 07 '13 14:02

Gleeb


People also ask

What is FreeMarker Template Error?

Description. Description: FreeMarker template error appears in logs when attempting to add a Web Content Article to a page or search for it in the Search portlet. The issue only occurs when the Summary field is empty and when using the following line in the template.

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.


3 Answers

Have a look at

void setClassForTemplateLoading(Class cl, String prefix); 

...in the FreeMarker manual chapter about template loading.

Example:

cfg.setClassForTemplateLoading(this.getClass(), "/templates"); 

...if your templates is located in the templates package relative to the root of the current class.

like image 191
Werner Kvalem Vesterås Avatar answered Sep 28 '22 09:09

Werner Kvalem Vesterås


Configuration cfg;
private Template template;  
    {
        cfg=new Configuration(); 
        try {
            cfg.setClassForTemplateLoading(this.getClass(), "/templates");
            template = cfg.getTemplate("template.ftl");
}

This worked perfectly for me. Here my templates folder contains template.ftl which is under src/main/resources package.

like image 31
Shiv Avatar answered Sep 28 '22 10:09

Shiv


An alternative to the mentioned

cfg.setClassForTemplateLoading(this.getClass(), "/templates");

is

TemplateLoader ldr = new ClassTemplateLoader(classLoader, basePackagePath);
cfg.setTemplateLoader(ldr);

which can be useful if you need to load stuff from other jars than the one your ftl processor belongs to.

Calls to cfg.getTemplate(..) will then perhaps be more convenient as they only need the path to the ftl relative to basePackagePath

like image 21
LOAS Avatar answered Sep 28 '22 08:09

LOAS