I have some user provided text which I'd like to display correctly in browser (with new lines replaced with <br/>
-tags). I wish to have every other HTML tag escaped. What's the best way to do this?
Not the prettiest solution, but this will work. If the view accepts a string parameter str
, then @Html(HtmlFormat.escape(str).toString.replace("\n", "<br />"))
will first escape the string, then replace all instances of \n with <br />
, and lastly transform it back to html so that Play doesn't try to escape already escaped characters. If you want to also escape \r or \r\n you could chain another replace
onto that, or use a regex instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With