Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process string templates with thymeleaf 3

Can we use StringTemplateResolver to populate a string template with Icontext. If so how we can do? TemplateProcessingParameters and IResourceResolver is removed from Thymeleaf 3. Any working example would greatly help?

I have followed this example and it works great in Thymeleaf 2
Is there a way to make Spring Thymeleaf process a string template?

I didnt see any reference any migration guide as well.

like image 517
jslearner07 Avatar asked Aug 21 '16 09:08

jslearner07


1 Answers

I think I found a solution. If anybody has better answer please let me know. I did a small mistake earlier. Hope this helps.

private TemplateEngine templateEngine;

private TemplateEngine getTemplateEngine() {
        if(null == templateEngine){
            templateEngine = new TemplateEngine();
            StringTemplateResolver templateResolver =new   StringTemplateResolver();
            templateResolver.setTemplateMode(TemplateMode.HTML);
            templateEngine.setTemplateResolver(templateResolver);
        }
        return templateEngine;
    }




public String getTemplateFromMap(String htmlContent, Map<String, String> dynamicAttibutesMap) {
    templateEngine = getTemplateEngine();
    String template = null;
    final Context ctx = new Context(new Locale(TEMPLATE_LOCAL));
    if (!CollectionUtils.isEmpty(emailAttibutesMap)) {
        dynamicAttibutesMap.forEach((k,v)->ctx.setVariable(k, v));
    }
    if (null != templateEngine) {
        template = templateEngine.process(htmlContent, ctx);
    } 
    return template;
}
like image 123
jslearner07 Avatar answered Sep 21 '22 18:09

jslearner07