gstackoverflow
gstackoverflow

Reputation: 37002

How to render raw html in freemarker?

I have template like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>

<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;">

    <p>server response: ${response}</p>
    ...
</table>

</body>
</html>

actually server can obtain any response.

In some cases server returns html and freemarker parse it as html and it looks bad.

I want to show user row html with tags and so on like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

How can I do it?

Upvotes: 1

Views: 3504

Answers (1)

ddekany
ddekany

Reputation: 31162

Turn HTML auto-escaping on, so ${response} will not be rendered as HTML (and so that you avoid XSS vulnerabilities). Starting the file with <#ftl output_format="HTML"> will do that. Though the recommended way is using ftlh file extension and then either setting incompatible_improvements to at least 2.3.24, or setting recognize_standard_file_extensions to true.

In case you don't want to use auto-escaping for some reason, you can write ${response?html}.

As white-space shouldn't be collapsed by the browser, you certainly also want to put that interpolation into a HTML pre or textarea, or into a div with some class that has white-space: pre-wrap.

Upvotes: 5

Related Questions