当我调用Go模板函数输出HTML时,它会显示ZgotmplZ
.
示例代码:
http://play.golang.org/p/tfuJa_pFkm
package main
import (
"html/template"
"os"
)
func main() {
funcMap := template.FuncMap{
"printSelected": func(s string) string {
if s == "test" {
return `selected="selected"`
}
return ""
},
"safe": func(s string) template.HTML {
return template.HTML(s)
},
}
template.Must(template.New("Template").Funcs(funcMap).Parse(`
<option {{ printSelected "test" }} {{ printSelected "test" | safe }} >test</option>
`)).Execute(os.Stdout, nil)
}
Run Code Online (Sandbox Code Playgroud)
输出:
<option ZgotmplZ ZgotmplZ >test</option>
Run Code Online (Sandbox Code Playgroud)
Joe*_*Joe 24
"ZgotmplZ"是一个特殊值,表示不安全内容在运行时到达CSS或URL上下文.该示例的输出将是:
<img src="#ZgotmplZ">
Run Code Online (Sandbox Code Playgroud)
您可以向模板funcMap添加safe和attr函数:
包主
import (
"html/template"
"os"
)
func main() {
funcMap := template.FuncMap{
"attr":func(s string) template.HTMLAttr{
return template.HTMLAttr(s)
},
"safe": func(s string) template.HTML {
return template.HTML(s)
},
}
template.Must(template.New("Template").Funcs(funcMap).Parse(`
<option {{ .attr |attr }} >test</option>
{{.html|safe}}
`)).Execute(os.Stdout, map[string]string{"attr":`selected="selected"`,"html":`<option selected="selected">option</option>`})
}
Run Code Online (Sandbox Code Playgroud)
输出结果如下:
<option selected="selected" >test</option>
<option selected="selected">option</option>
Run Code Online (Sandbox Code Playgroud)
您可能想要定义一些其他函数,可以将字符串转换为template.CSS,template.JS,template.JSStr,template.URL等.
package main
import (
"html/template"
"os"
)
type T struct {
HTML template.HTML
ATTR template.HTMLAttr
URL template.URL
JS template.JS
CSS template.CSS
}
func main() {
data := T{
HTML: `<div>test div</div>`,
ATTR: `selected="selected"`,
URL: `https://upload.wikimedia.org/wikipedia/commons/5/53/Google_%22G%22_Logo.svg`,
CSS: `font-size: 15px`,
JS: `console.log("hello world")`,
}
template.Must(template.New("Template").Parse(`
{{.HTML}}
<option {{.ATTR}} style="{{.CSS}}">test</option>
<script>{{.JS}}</script>
<img src="{{.URL}}">
`)).Execute(os.Stdout, data)
}
Run Code Online (Sandbox Code Playgroud)
输出
<div>test div</div>
<option selected="selected" style="font-size: 15px">test</option>
<script>console.log("hello world")</script>
<img src="https://upload.wikimedia.org/wikipedia/commons/5/53/Google_%22G%22_Logo.svg">
Run Code Online (Sandbox Code Playgroud)
您试图在 template/html 认为不安全的地方输出 HTML(例如,在 HTML 元素内,如下所示:
<option {{ printSelected }}>
Run Code Online (Sandbox Code Playgroud)
我找不到任何方法来说服它是安全的(包括返回 template.HTML 而不是字符串);我发现的唯一替代方法是重写模板,在本例中使用 bool 输出代替:
<option {{ if printSelected }}selected{{ end }}>
Run Code Online (Sandbox Code Playgroud)
小智 5
我<img src="{{myfunction}}">
在myfunction返回编码图像的地方遇到了类似的问题。
最后我解决了它,而不是返回字符串函数template.URL(mystring)
。