Search

Dark theme | Light theme

February 27, 2009

Show HTML as HTML in Grails

Starting with Grails on a little project I already learned something. In a domain object, Wish, I had a String property, description, which can contain HTML code. For example we can use the following value for the description property: This description is<br />two lines..

Following the Grails reference documentaiton I created a GSP file and used ${fieldValue(bean: wish, property: 'description'} to output the value. When I look a the output I got:

This description is <br />two lines.

The HTML was already encoded and the < and > brackets where replaced by &lt; and &gt; automatically.

I changed the GSP file and used ${wish.description} and I got what I wanted:

This description is
two lines.

We can use encodeAsHTML() and decodeHTML() methods to get the same effect. So for example if we use ${wish.property.encodeAsHTML() the < and > brackets are automatically replaced again.