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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With