I've recently swapped out datastores and as a side-effect have had to change a struct field from template.HTML
to string
to be compatible with the marshaller/DB driver. This field, RenderedDesc
, contains rendered HTML as passed through russross/blackfriday.
Previously I could just pass the whole struct into the template "as is" and call {{ .RenderedDesc }}
in the template.
Because it's now a string, I've added a filter to convert it back on template render:
templates.go
func RenderUnsafe(s string) template.HTML {
return template.HTML(s)
}
template.FuncMap{
...
"unsafe": RenderUnsafe,
}
_content.tmpl
...
<div class="detail">
{{ .RenderedDesc | unsafe }}
</div>
...
Is there a better way to achieve this without having to use a filter at the template level? Short of re-writing marshalling logic from my DB driver (not on the cards) it looks like this is the simplest way to "store" strings but render raw HTML.