将 html 注入 thymeleaf 模板 [英] Inject html into thymeleaf template
问题描述
我在数据库中有百里香模板,
I have thymeleaf templates lying in database,
首先我检索模板并对其进行处理:
First I retrieve template and process it:
String processedTemplate = templateEngine.process(databaseTemplate, context);
所以现在 processedTemplate
包含 html
作为 String
.
So now processedTemplate
contains html
as a String
.
然后我检索另一个模板并执行基本相同的操作,但我还想将以前的模板注入其中,因此 java 代码应如下所示:
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);
那么我应该在我的 mainTemplate
中放入什么才能通过 Context
注入另一个 html?
So what should I put into my mainTemplate
to be able to inject another html via Context
into it?
我看到了这样的东西:
<div th:replace="fragments/header :: header">Header</div>
但它适用于文件中的模板,但不适用于数据库中的模板.
But it works with templates from file, but not when they are lying in database.
推荐答案
听起来你想在没有 HTML 转义的情况下插入文本,你用 th:utext
做到了:
It sounds that you want to insert text without HTML escaping, you do that with th:utext
:
<div th:utext="${htmlToInject}"></div>
或者使用内联:
[(${htmlToInject})]
这篇关于将 html 注入 thymeleaf 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!