1N5*_*818 4 html templates json escaping go
我在golang中有一个模板,我有一个看起来像这样的字符串:
<some_html> {{ .SomeOtherHTML }} </some_html>
Run Code Online (Sandbox Code Playgroud)
我期待输出是这样的:
<some_html> <the_other_html/> </some_html>
Run Code Online (Sandbox Code Playgroud)
但相反,我看到这样的事情:
<some_html> <the_other_html/< </some_html>
Run Code Online (Sandbox Code Playgroud)
我也试图插入一些JSON,但golang正在逃避角色并添加一些"他们不应该的地方.
如何在golang中插入HTML模板而不发生这种情况?
dav*_*ave 11
您应该将变量作为a template.HTML而不是作为string:
tpl := template.Must(template.New("main").Parse(`{{define "T"}}{{.Html}}{{.String}}{{end}}`))
tplVars := map[string]interface{} {
"Html": template.HTML("<p>Paragraph</p>"),
"String": "<p>Paragraph</p>",
}
tpl.ExecuteTemplate(os.Stdout, "T", tplVars)
//OUTPUT: <p>Paragraph</p><p>Paragraph</p>
Run Code Online (Sandbox Code Playgroud)
https://play.golang.org/p/QKKpQJ7gIs
正如您所看到的,我们作为a传递的变量template.HTML不会被转义,而是作为a传递的变量string.
| 归档时间: |
|
| 查看次数: |
2518 次 |
| 最近记录: |