去模板.ExecuteTemplate包含html

Pet*_*ter 24 go go-templates

我已经按照本教程:http://golang.org/doc/articles/wiki/final.go并根据我的需要稍微修改了它.问题是我想在模板中支持HTML.我意识到这是一个安全风险,但目前并不是一个问题.

页面呈现的结果:

<h1>this<strong>is</strong>a test</h1>
Run Code Online (Sandbox Code Playgroud)

让我解释一下代码:

type Page struct {
    Title string
    Body  []byte
}
Run Code Online (Sandbox Code Playgroud)

我想要存储HTML的数据存储在Page.Body.这是类型[]byte,这意味着我不能(或我可以?)运行,html/template.HTML(Page.Body)因为该函数需要一个字符串.

我有这个预呈现模板:

var (
    templates = template.Must(template.ParseFiles("tmpl/edit.html", "tmpl/view.html"))
)
Run Code Online (Sandbox Code Playgroud)

而实际ExecuteTemplate看起来像这样:

err := templates.ExecuteTemplate(w, tmpl+".html", p)
Run Code Online (Sandbox Code Playgroud)

其中w是w http.ResponseWriter,tmpl是tmpl string,而p是p *Page

最后我的'view.html'(模板)如下所示:

<h1>{{.Title}}</h1>
<p>[<a href="/edit/{{.Title}}">edit</a>]</p>
<div>{{printf "%s" .Body}}</div>
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

  • {{printf "%s" .Body | html}} 什么都不做
  • 我已经包含github.com/russross/blackfriday(Markdown处理器)并运行p.Body = blackfriday.MarkdownCommon(p.Body)了正确将Markdown转换为HTML,但HTML仍然作为实体输出.
  • 编辑:我尝试了下面的代码(我不知道为什么格式混乱),它仍然输出完全相同.

    var s template.HTML s = template.HTML(p.Body) p.Body = []byte(s)

非常感谢任何指导.如果我感到困惑,请问我可以修改我的问题.

thw*_*hwd 51

转换您的[]bytestring类型template.HTML(此处记录)

p.Body = template.HTML(s) // where s is a string or []byte
Run Code Online (Sandbox Code Playgroud)

然后,在您的模板中,只需:

{{.Body}}
Run Code Online (Sandbox Code Playgroud)

它将被打印而不会逃脱.

编辑

为了能够在您页面的正文中包含HTML,您需要更改Page类型声明:

type Page struct {
    Title string
    Body  template.HTML
}
Run Code Online (Sandbox Code Playgroud)

然后分配给它.

  • 不要只是复制粘贴,并说它不起作用.您显然需要更改Body字段的类型,以便能够为其指定template.HTML类型的内容. (8认同)

tux*_*21b 16

看一下template.HTML类型.它可以用于封装HTML的已知安全片段(如Markdown的输出)."html/template"包不会逃避此类型.

type Page struct {
    Title string
    Body template.HTML
}

page := &Page{
    Title: "Example",
    Body:  template.HTML(blackfriday.MarkdownCommon([]byte("foo bar")),
}
Run Code Online (Sandbox Code Playgroud)

我通常编写自己的func Markdown(text string) html.Template方法,使用适当的配置调用blackfriday并执行某些类型的转换.另一个替代方案也可能是在模板解析器中注册一个"html"函数,它允许您通过执行类似操作来输出任何值而不进行任何转义{{html .MySafeStr}}.代码可能如下所示:

var tmpl = template.Must(template.New("").Funcs(template.FuncMap{
    "html": func(value interface{}) template.HTML {
        return template.HTML(fmt.Sprint(value))
    },
}).ParseFiles("file1.html", "file2.html"))
Run Code Online (Sandbox Code Playgroud)


maj*_*rif 10

我为模板创建了一个自定义函数,如下所示:

func noescape(str string) template.HTML {
    return template.HTML(str)
}

var fn = template.FuncMap{
    "noescape": noescape,
}
Run Code Online (Sandbox Code Playgroud)

然后在你的模板上:

{{ noescape $x.Body }}
Run Code Online (Sandbox Code Playgroud)