Sometimes we need to show the HTML when displaying data. This post shows how to escape HTML in Liferay Freemarker.

These are some of the ways to escape HTML in Freemarker templates in Liferay.

Using Liferay Util to escape HTML

We can use HtmlUtil in both Freemarker and Velocity, so this code should work on both:

<#-- Assuming that curEntry.getContent() returns HTML  -->
${htmlUtil.escape(curEntry.getContent())}

Using Freemarker to escape HTML

Liferay 6.2 and Lower

If we are using Liferay 6.2 or below, then we need to use the old Freemarker method for escaping HTML:

<#escape x as x?html> ${curEntry.getContent()} </#escape>

Liferay 7

I believe Liferay 7 is using the latest version of Freemarker, so the method is different:

${curEntry.getContent()?esc}

 For references and more details on what else can we escape, view the following sites:

To see more of our posts on Liferay, please visit: Liferay posts.

Categories: Liferay

2 Comments

Jyothi · April 18, 2019 at 6:51 am

How to escape HTML in velocity template with liferay 7???. I have used ${htmlUtil.escape(curEntry.getContent())} but it is not working.Please help me.

    waqasaslam · April 20, 2019 at 1:56 am

    Escape by adding the ?esc option to the html, like the following:
    ${curEntry.getContent()?esc}

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *