NIshank
NIshank

Reputation: 127

How do I bind string which is HTML in FreeTextMarker?

I need to bind couple of strings and few html snippets. Output HTML should contain data along with HTML(in design).

Upvotes: 0

Views: 222

Answers (2)

ddekany
ddekany

Reputation: 31162

Let's say your data-model is a Map<String, Object>, called model (could be bean with getters too of course). Then you should fill the mode like this:

model.put("htmlSnippet", HTMLOutputFormat.INSTANCE.fromMarkup("This is <em>HTML</em>!");
model.put("plainTextSnippet", "This is <em>not</em> HTML!");

Above, htmlSnippet will be a TemplateHTMLOutputModel object, not a String, so FreeMarker will know that it mustn't be HTML-escaped.

Now if your template is this, and you have HTML auto-escaping enabled (typically done by giving ftlh file extension, or with <#ftl outputFormat='HTML'> header, or otherwise in the Configuration):

${htmlSnippet}
${plainTextSnippet}

Then the HTML source of the output will be this:

This is <em>HTML</em>!
This is &lt;em&gt;not&lt;/em&gt; HTML!

Upvotes: 1

walidum
walidum

Reputation: 173

You can use StringBuilder to get your email-html as String.

Upvotes: 0

Related Questions