I am trying to render an HTML file using thymeleaf and keep the resultant HTML content in a String variable in web-based scopes of Spring
so that i can use it l
I'm using similar Springboot and Thymeleaf versions and something like this worked for me on a few projects :
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@Component
public class EmailProcessor {
private TemplateEngine htmlTemplateEngine;
@Autowired
public EmailProcessor(TemplateEngine templateEngine) {
this.htmlTemplateEngine = templateEngine;
}
public String process(User user) {
final Context ctx = new Context();
if (user != null) {
ctx.setVariable("user", user);
}
return htmlTemplateEngine.process("emails/template", ctx);
}
}
Email template is just a regular Thymeleaf template which is located in:
resouces/templates/emails/template.html