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.具有自定义功能
您可以使用自定义功能。
创建一个简单的函数,将
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)) |
仅用于演示目的,
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.修改
如果可以的话,将
3.在
您还可以向
1 2 3 | func (a *Article) SafeBody() template.HTML { return template.HTML(a.BodyHTML) } |
有了此方法,您可以简单地从模板中调用它:
1 2 3 4 | {{range .}} {{.Title}} {{.SafeBody}} {{end}} |
在Go Playground上尝试使用此变体。