2

構造体に配列フィールドがある場合、HTML をエスケープするにはどうすればよいですか?

単一のショー ページの場合、次のコードが機能します。

show.go:

err := ShowTmpl.ExecuteTemplate(w, "show.html", struct {
        Title    string
        SafeBody template.HTML
}{
    t.Title,
    template.HTML(t.BodyHTML),
})

インデックス ページの場合:

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}}

構造体フィールドにまたがっているときに HTML をエスケープするにはどうすればよいですか?

4

1 に答える 1