How to use HTML in grails messages.properties for mail sending
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开发者_StackOverflow>
${variableWithHTMLTextFromMessagesProperties}
</body>
</html>
Can you not do the localisation in the GSP using the message tag, similar to the following? Controller -
sendMail {
to "my@email.com"
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?
Found the solution here. Easiest way is just to use <%=variableWithHTMLTextFromMessagesProperties%>
instead of ${variableWithHTMLTextFromMessagesProperties}
. This stops the HTML escaping.
I made my own solution with a custom taglib.
def htmlMessage = { attrs, body ->
out << g.message(attrs, body).decodeHTML()
}
Then to define a message, it has to be with escaped html:
my.html.message={0,choice,0#There is no elements|1#There is <strong>1</strong> element|1<There are <strong>{0}</strong> elements}
For html just:
<g:htmlMessage code="my.html.message" args="[qElements]" />
The result is an i18n html generated with number in strong font. Like this:
"There are 9 elements"
From the docs:
<g:encodeAs codec="HTML">
Profile for user ${user.name} is:
<g:render template="/fragments/showprofile"/>
</g:encodeAs>
精彩评论