关于go:在struct的数组字段中转义HTML

Escape HTML in array field in struct

当结构中有数组字段时,如何转义HTML?

对于单个显示页面,此代码有效:

show.go:

1
2
3
4
5
6
7
err := ShowTmpl.ExecuteTemplate(w,"show.html", struct {
        Title    string
        SafeBody template.HTML
}{
    t.Title,
    template.HTML(t.BodyHTML),
})

对于索引页:

index.go

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
3
4
5
6
{{with .Articles}}
  {{range .}}
    {{.Title}}
    {{.BodyHTML | html}} // Doesn't work
  {{end}}
{{end}}

当我遍历struct字段时如何转义HTML?


您可以通过多种方式实现这一目标:

1.具有自定义功能

您可以使用自定义功能。 Template.Funcs()方法允许您注册可以从模板调用的任何自定义函数。

创建一个简单的函数,将string转换为template.HTML,如下所示:

1
2
3
func ToHtml(s string) template.HTML {
    return template.HTML(s)
}

您可以这样注册:

1
2
t := template.Must(template.New("index.html").
    Funcs(map[string]interface{}{"ToHtml": ToHtml}).Parse(indexHtml))

仅用于演示目的,indexHtml是模板的string

1
2
3
4
5
6
7
const indexHtml = `{{with .}}
  {{range .}}
    {{.Title}}
    {{ToHtml .BodyHTML}}
  {{end}}
{{end}}
`

您可以引用它,并从模板中调用它,如下所示:

1
{{ToHtml .BodyHTML}}

使用以下参数调用此模板:

1
2
3
4
5
6
a := []struct {
    Title    string
    BodyHTML string
}{{"I'm the title","I'm some HTML code!"}}

err := t.ExecuteTemplate(os.Stdout,"index.html", a)

这是Go Playground上完整的工作示例。

2.修改Article

如果可以的话,将Article.BodyHTML的类型更改为template.HTML会更容易,然后将其转义为不转义,而无需再费力气了。这也将使意图变得清晰(它应该/确实包含将不转义呈现的安全HTML)。

3.在Article中添加方法

您还可以向Article类型添加一个方法,该方法将以template.HTML的形式返回其BodyHTML字段(自定义函数在命题1中的作用差不多):

1
2
3
func (a *Article) SafeBody() template.HTML {
    return template.HTML(a.BodyHTML)
}

有了此方法,您可以简单地从模板中调用它:

1
2
3
4
  {{range .}}
    {{.Title}}
    {{.SafeBody}}
  {{end}}

在Go Playground上尝试使用此变体。