Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

So now processedTemplate contains html as a String.

Then I retrieve another template and do basically 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.

like image 797
Ruslan Akhundov Avatar asked Aug 01 '17 08:08

Ruslan Akhundov


1 Answers

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})]
like image 123
holmis83 Avatar answered Oct 08 '22 06:10

holmis83