Golang如何使用模板引擎渲染HTML

html/template 默认对变量输出做HTML转义防XSS,text/template不转义;html/template校验标签闭合,后者不校验;渲染HTML必须用前者,并设Content-Type。

Go 的 html/templatetext/template 有什么区别?

直接用 html/template,别选 text/template 渲染 HTML。前者默认对变量输出做 HTML 转义(比如把 变成 zuojiankuohaophpcn),防止 XSS;后者不转义,容易出安全问题。

除非你明确要输出纯文本(如生成配置文件、邮件正文),否则一律用 html/template

  • html/template{{.Name}} 会自动转义 >&"'
  • 若需原样插入 HTML(比如后端拼好的 safeHTML 字段),必须用 {{.Content | safeHTML}},且该字段类型得是 template.HTML
  • 加载模板时,html/template 会校验标签闭合,text/template 不校验 —— 这意味着用错包可能让页面渲染出错但无提示

怎么加载并渲染一个 HTML 模板文件?

典型流程:解析文件 → 构建数据 → 执行写入 http.ResponseWriterbytes.Buffer

注意路径是相对于 os.Getwd()(运行目录),不是源码目录;推荐用 embed(Go 1.16+)避免部署时漏模板文件。

package main

import (
    "html/template"
    "net/http"
    "embed"
)

//go:embed templates/*.html
var templatesFS embed.FS

func handler(w http.ResponseWriter, r *http.Request) {
    t := template.Must(template.ParseFS(templatesFS, "templates/*.html"))
    data := struct{ Title string }{Title: "首页"}
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    t.Execute(w, data)
}
  • template.ParseFiles("index.html") 简单但易因路径出错;ParseFS 更健壮
  • template.Must() 在解析失败时 panic,适合启动期加载;线上服务建议自己捕获 error
  • 务必手动设置 Content-Type,否则浏览器可能误判编码,中文变乱码

如何在模板里安全插入动态 HTML?

不能直接写 {{.RawHTML}},那会被转义成字符串字面量。必须显式标记为“可信”:

type PageData struct {
    Title   string
    Content template.HTML // 类型必须是 template.HTML
}

func handler(w http.ResponseWriter, r *http.Request) {
    data := PageData{
        Title:   "公告",
        Content: template.HTML(`

欢迎

加粗`), } t := template.Must(template.New("").Parse(`

{{.Title}}

{{.Content}}`)) t.Execute(w, data) }
  • 字段类型必须是 template.HTML,不是 string
  • 不要在模板里用 {{.Content | printf "%s"}} 之类绕过 —— 不生效,且破坏安全模型
  • 如果 HTML 来自用户输入,千万别转成 template.HTML!应先用 bluemonday 等库净化

常见报错和调试技巧

模板执行失败通常静默返回空内容或 500,很难定位。关键错误类型:

  • template: xxx:xxx: unexpected EOF:模板文件末尾缺 {{end}} 或括号不匹配
  • template: xxx:xxx: nil pointer evaluating interface {}.Field:传了 nil 结构体或字段未导出(首字母小写)
  • 页面显示 {{.Name}} 原样字符串:没调 Execute,或 Execute 返回 error 但被忽略
  • 中文乱码:响应头没设 Content-Type: text/html; charset=utf-8,或模板文件本身不是 UTF-8 编码

调试建议:先用 bytes.Buffer 替代 http.ResponseWriter,打印出错时的完整 error 和输出内容。