How do I unescape html?
I'm passing a css file into html like this
<style>{{.file}}</style>
I get this
<style>ZgotmplZ</style>
I've tried wrapping the field with template.HTML(data), didn't work.
How do I unescape html?
I'm passing a css file into html like this
<style>{{.file}}</style>
I get this
<style>ZgotmplZ</style>
I've tried wrapping the field with template.HTML(data), didn't work.
The Go HTML template package properly excapes CSS. Quoting from the documentation of the
template
package:"ZgotmplZ"
is a special value, it is used as a replacement if the value you're trying to include is invalid or unsafe in the context.So the problem is the CSS value you're trying to include, it is not safe. Try something simple first and see if it works, like:
Found the discussion of
"ZgotmplZ"
in the documentation (at typeErrorCode
), quoting it:"ZgotmplZ"
explanation:Example Template:
Discussion:
Solution
Since the code you try to insert is in the context of CSS code and not HTML, you can't/shouldn't use
template.HTML(data)
.There is a predefined type
CSS
for safe inclusion of CSS code coming from trusted source, e.g. CSS code you specify and is not coming from an HTML form filled by the user. Example:And pass the
safeCss
value to your template parameter.