2

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?

4

1 回答 1

2

您可以通过多种方式实现此目的:

1.具有自定义功能

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

创建一个简单的函数,将 a 转换stringtemplate.HTML这样的:

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

您可以像这样注册它:

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

仅用于演示目的indexHtmlstring您的模板:

const indexHtml = `{{with .}}
  {{range .}}
    <a href="/">{{.Title}}</a>
    {{ToHtml .BodyHTML}}
  {{end}}
{{end}}
`

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

{{ToHtml .BodyHTML}}

使用参数调用此模板:

a := []struct {
    Title    string
    BodyHTML string
}{{"I'm the title", "I'm some <b>HTML</b> code!"}}

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

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

2.修改Article

如果可以的话,只需更改 to 的类型会更容易,Article.BodyHTML然后template.HTML它会毫不费力地呈现为未转义。这也将使意图明确(它应该/确实包含安全的 HTML,它将被呈现为非转义)。

3.添加方法Article

您还可以向Article类型添加一个方法,该方法将其BodyHTML字段作为 a返回template.HTML(几乎与命题 #1 中的自定义函数所做的一样):

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

有了这个方法,你可以简单地从模板中调用它:

  {{range .}}
    <a href="/">{{.Title}}</a>
    {{.SafeBody}}
  {{end}}

在Go Playground上试试这个变体。

于 2015-04-20T07:31:14.417 回答