逃避'到'在golang html模板中 [英] escaping ' to ' in golang html template
本文介绍了逃避'到'在golang html模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何防止在html模板中转义'
至'
:
How to prevent escaping '
to '
in html template:
package main
import (
"html/template"
"os"
)
const tmpl = `<html>
<head>
<title>{{.Title}}</title>
</head>
</html>`
func main() {
t := template.Must(template.New("ex").Parse(tmpl))
v := map[string]interface{}{
"Title": template.HTML("Hello World'"),
}
t.Execute(os.Stdout, v)
}
输出:
It outputs:
<html>
<head>
<title>Hello World'</title>
</head>
</html>
所需输出:
Desired output:
<html>
<head>
<title>Hello World'</title>
</head>
</html>
推荐答案
@dyoo清楚地解释了< title>
内容被视为RCDATA。转义代码是此处。分支 if t == contentTypeHTML
是 template.HTML
所发生的情况。
@dyoo has explained clearly that <title>
content is treated as RCDATA. The code that does the escaping is here. The branch if t == contentTypeHTML
is what happens with template.HTML
.
如果您确实需要控制源代码的输出,请使用 text / template
并手动进行转义。
If you really need to control the output of the source, use text/template
and do the escaping manually.
这篇关于逃避'到&#39;在golang html模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文