I’m loading a text file that has newlines in it, and pass it to html/templates.
Substituting the \n with <br> in the loaded string, they are escaped by the template to html <br> and displayed in the browser, instead of causing a line return.
How can I change this behavior without switching to text/templates (which doesn’t have XSS protection)?
It seems you could run template.HTMLEscape() on your text first to sanitize it, then do the \n to
substitution that you trust, then use that as pre-escaped and trusted template data.
Update: Expanding on Kocka’s example, this is what I had in mind:
http://play.golang.org/p/JiH0uD5Zh2
Output is
And text rendered in the browser is