How do I escape HTML when I have an array field in a struct?
For a single show page, this code works:
show.go:
err := ShowTmpl.ExecuteTemplate(w, "show.html", struct {
Title string
SafeBody template.HTML
}{
t.Title,
template.HTML(t.BodyHTML),
})
For an index page:
index.go
type as struct {
Articles []*Article
}
var a as
// some code to give a.Articles its values
err := IndexTmpl.ExecuteTemplate(w, "index.html", a)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
index.html:
{{with .Articles}}
{{range .}}
<a href="/">{{.Title}}</a>
{{.BodyHTML | html}} // Doesn't work
{{end}}
{{end}}
How do I escape HTML when I'm ranging over a struct field?