Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output string as html in freemarker

So we are storing html in out data model. I need to output this into a freemarker template:

example:

[#assign value = model.value!]
${value}

value = '<p>This is <a href='somelink'>Some link</a></p>'

I have tried [#noescape] but it throws an error saying there is no escape block. see FREEMARKER: avoid escaping HTML chars. This solution did not work for me.

like image 308
Katherine C Avatar asked Feb 24 '15 15:02

Katherine C


People also ask

How do I print a value in FreeMarker?

For example, if you create a variable called "foo" in the template, you can print its value with ${foo} . If, coincidently, there's a variable called "foo" in the data-model too, the variable created in the template will hide (not overwrite!)

What is FreeMarker HTML?

Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data.

How do you escape characters in FreeMarker?

esc creates a markup output value out of a string value by escaping all special characters in it.

What is HTML FTL?

FTL tags (for FreeMarker Template Language tags): FTL tags are a bit similar to HTML tags, but they are instructions to FreeMarker and will not be printed to the output. The name of these tags start with # . (User-defined FTL tags use @ instead of # , but they are an advanced topic.)


1 Answers

[#noescape] or <#noescape> is only valid when used inside an [#escape] tag. Your data is probably stored with the HTML encoded. You need to get the backend to un-encode the html.

Otherwise you'll need to do something like...

${value?replace("&gt;", ">")?replace("&lt;", "<")}

But that isn't a good approach because it won't catch all the encoded values and shouldn't be done in the view layer.

like image 67
ratherblue Avatar answered Oct 24 '22 18:10

ratherblue