views:

55

answers:

3

In grails, I use a GSP template to render an HTML email sent out with the mail plug-in. This works fine, however the GSP template uses a param which is in turn retrieved from my messages.properties file. Now I want to use HTML, e.g. <br/> inside the messages.properties, but it in the mail it appears as text and the tag is not interpreted.

I already tried to use .decodeHTML() on the parameter in the GSP, but it didn't work.

Where do I have to encode / decode or is it possible at all to use HTML tags inside messages.properties?

<%@ page contentType="text/html"%>
<html>
<body>
${variableWithHTMLTextFromMessagesProperties}
</body>
</html>
A: 

From the docs:

<g:encodeAs codec="HTML">
   Profile for user ${user.name} is:
   <g:render template="/fragments/showprofile"/>
</g:encodeAs>
Brian
This works in the wrong direction; now the <br/> in the messages.properties is displayed as <br/>
werner5471
+1  A: 

Can you not do the localisation in the GSP using the message tag, similar to the following? Controller -

sendMail {
    to "[email protected]"
    subject "cheese"
    html g.render(template:"myTemplate")
}

And then in your _myTemplate.gsp -

<%@ page contentType="text/html" %>
<html><head></head>
<body>
    <p>test: <g:message code="a.test"/></p>
</body>
</html>

And then in messages.properties -

a.test=this <br/><br/> is a test

This way works fine for me (Grails 1.3.1, mail 0.9), I get 2 line breaks in the html email received. Any reason you can't do it this way?

Chris
Actually in my case the text from messages.properties is saved as a String in a persisted class beforehand, and this is then passed to the view. Of course I could theoretically store the message code instead, but then I'd also need to store the parameters that should be used later on, so I'd rather go for storing the message directly... But I guess I'm gonna use your solution as a fallback solution if it doesn't work any other way, so thank you!
werner5471
+1  A: 

Found the solution here. Easiest way is just to use <%=variableWithHTMLTextFromMessagesProperties%> instead of ${variableWithHTMLTextFromMessagesProperties}. This stops the HTML escaping.

werner5471