gpt4 book ai didi

go - 在golang html模板中转义'to'

转载 作者:IT王子 更新时间:2023-10-29 01:13:43 24 4
gpt4 key购买 nike

如何防止在 html 模板中将 ' 转义为 ':

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)
}

输出:

<html>
<head>
<title>Hello World&#39;</title>
</head>
</html>

期望的输出:

<html>
<head>
<title>Hello World'</title>
</head>
</html>

playouground

最佳答案

@dyoo 已经解释清楚 <title>内容被视为 RCDATA。进行转义的代码是 here .分公司if t == contentTypeHTML template.HTML 会发生什么.

如果你真的需要控制源的输出,使用text/template并手动转义。

关于go - 在golang html模板中转义'to',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com