Supriya
Supriya

Reputation: 141

how to show html in grails?

I have a domain class that has a string variable containing HTML code. I want to display this code on myxml.gsp.

For this I've written the following code:

Learn.groovy

public class Learn {

    String content = ("<html><head><title>Learning Grails</title>
       <body>Grails is fun</body></html>");

}

myxml.gsp

<html>
  <head>
    <title>Xml Output</title>
  </head>
  <body>
     <p>${learn.content}<p> 
  </body>
</html>

However it is unable to fetch value for "content", getting the error: "Cannot get property 'content' on null object". What I am doing wrong? Is there any better way to accomplish this?

Upvotes: 1

Views: 5938

Answers (2)

LiCheng
LiCheng

Reputation: 929

I use this:

${raw(htmlString)}

Upvotes: 13

Victor Sergienko
Victor Sergienko

Reputation: 13475

If you need to display HTML markup to user, use ${learn.content.encodeAsHTML()}.

For Cannot get property 'content' on null object error, looks you're passing a null learn object or not passing it at all. Check that you have something like: [...(something), learn: learnInstance] returned from your action.

If it's OK to have null learn in GSP (which I don't recommend) - use ${learn?.content?.encodeAsHTML()}.

Upvotes: 4

Related Questions