有没有办法在 Golang html 模板 ( ) 中将 Javascript 作为变量注入html/template。我期待脚本被注入到模板中,但是脚本被作为字符串注入到".
模板.html
...
<head>
{{ .myScript }}
</head>
...
解析器.go
...
fp := path.Join("dir", "shop_template.html")
tmpl, err := template.ParseFiles(fp)
if err != nil {
return err
}
return tmpl.Execute(writer, myObject{Script: "<script>console.log('Hello World!');</script>"})
...
呈现的 html 输出:
...
<head>
"<script>console.log('Hello World!');</script>"
</head>
...
预期产出
<head>
<script>console.log('Hello World!');</script>
// And should log Hello World! in the console.
</head>
萧十郎
相关分类