Martin Larizzate
Martin Larizzate

Reputation: 910

How to Load templates from external folder using Thymeleaf

I'm working with spring boot and thymeleaf to generate Documents from html templates.

As the templates continuously changes, i want ti to load templates from an external just to add or remove templates from there instead of redeploy the application.

As a POC, when using /resources folder works fine.

This is the error:

Error resolving template "voucher", the template might not exist or might not be accessible by any of the configured Template Resolvers

This is the context:

applycation.yml

spring:
  thymeleaf:
    prefix: file:///${PARAMETERS_DIRECTORY_TEMPLATES:/home/app/templates/}
    check-template-location: true
    suffix=: .html
    mode: HTML
    encoding: UTF-8

This is my Method: Where templateName is the template filename and parameters is just a map who has the values to be replaced by the engine.

 @Override
public String buildHtmlFromTemplate(String templateName, Map<String, String> parameters) {
    TemplateEngine templateEngine = new TemplateEngine();
    FileTemplateResolver templateResolver = new FileTemplateResolver ();
    templateResolver.setOrder(templateEngine.getTemplateResolvers().size());
    templateResolver.setCacheable(false);
    templateResolver.setCheckExistence(true);

    templateEngine.setTemplateResolver(templateResolver);

    return templateEngine.process(templateName, this.resolveHtmlTemplateAttributesContext(parameters));
}

NOTE: I removed tha applycation yml thymeleaf configs and implemented next code but the error persists.

@Override
public String buildHtmlFromTemplate(String templateName, Map<String, String> parameters) {
    TemplateEngine templateEngine = new TemplateEngine();
    FileTemplateResolver templateResolver = new FileTemplateResolver ();
    templateResolver.setPrefix("/home/skeeter/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    templateResolver.setOrder(templateEngine.getTemplateResolvers().size());
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);
    templateResolver.setCheckExistence(true);

    templateEngine.setTemplateResolver(templateResolver);

    return templateEngine.process(templateName, this.resolveHtmlTemplateAttributesContext(parameters));
}

Upvotes: 2

Views: 5673

Answers (2)

Martin Larizzate
Martin Larizzate

Reputation: 910

Curiously, the issue was solved using this code and the /usr/app/templates created with sudo. I think it was only a permissions issue

.....
@Value("${parameters.directory.templates}")
private String templatesDirectory;  
.....    


@Override
public String buildHtmlFromTemplate(String templateName, Map<String, String> parameters) {
    TemplateEngine templateEngine = new TemplateEngine();
    FileTemplateResolver templateResolver = new FileTemplateResolver ();
    templateResolver.setPrefix(templatesDirectory);
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    templateResolver.setOrder(templateEngine.getTemplateResolvers().size());
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);
    templateResolver.setCheckExistence(true);

    templateEngine.setTemplateResolver(templateResolver);

    return templateEngine.process(templateName, this.resolveHtmlTemplateAttributesContext(parameters));
}

Upvotes: 3

Fran Jimenez
Fran Jimenez

Reputation: 31

In my case I was using the ClassLoaderTemplateResolver, but after I changed it to FileTemplateResolver I can use the absolute path and it works fine.

Double check that you have read permissions in the directory where you have the templates.

Upvotes: 3

Related Questions