4

I'd like to pass a HTML text as a parameter to a message in a Twirl template, but when the template gets rendered the HTML gets escaped. For example, if my messages file has:

my.message=Click {0} to proceed

and my template.scala.html has

<p>@Messages("my.message", Html("<a href="/">here</a>"))</p>

when I open the page in a browser the <a href="/"here</a> part gets escaped, hence I don't have a clickable link. Is there any way to achieve that?

manub
  • 3,580
  • 2
  • 23
  • 33

1 Answers1

4

You should move your usage of Html to the outside, or keep it in both places.

@Html(Messages("my.message", "<a href=\"/\">here</a>"))

The problem is that the inner Html gets converted to a String when passed into Messages. Then the templates compiler sees that Messages returns a String, so it escapes it. Adding another Html on the outside will prevent that.

Michael Zajac
  • 53,182
  • 7
  • 105
  • 130