Inject html into thymeleaf template

感情迁移 提交于 2020-12-09 04:08:09

问题


I have thymeleaf templates lying in database,

First I retrieve template and process it:

String processedTemplate = templateEngine.process(databaseTemplate, context);

So now processedTemplate contains html as a String.

Then I retrieve another template and do basicly the same, but I want also inject previous template into it, so the java code should look like:

Context context = new Context(Locale.ENGLISH);
context.setVariable("htmlToInject", processedTemplated);
String result = templateEngine.process(mainTemplate, context);

So what should I put into my mainTemplate to be able to inject another html via Context into it?

I saw something like this:

<div th:replace="fragments/header :: header">Header</div>

But it works with templates from file, but not when they are lying in database.


回答1:


It sounds that you want to insert text without HTML escaping, you do that with th:utext:

<div th:utext="${htmlToInject}"></div>

Or with inlining:

[(${htmlToInject})]


来源:https://stackoverflow.com/questions/45433224/inject-html-into-thymeleaf-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!