在 go 的 `html/template` 中,使用 `{{ template "name" }}` 语法引入子模板时,默认不会自动继承当前作用域的数据;必须显式传入上下文(如 `.`)才能使
变量在被包含模板中可用。
当你在 index.tpl 中写 {{ template "head" }} 时,Go 模板引擎会以空数据(nil context) 渲染 "head" 模板——即 .Title 在 head.tpl 中求值为 "",导致
✅ 正确做法是:显式将当前数据(.)作为参数传递给被包含模板:
{{ template "head" . }}修改后的 index.tpl 片段如下:
{{ define "index" }}
{{ template "head" . }}
Main info:
Title: {{ .Title }}
Desc: {{ .Desc }}
{{ end }}此时 head.tpl 中的 {{ .Title }} 将能正确访问 Page{Title: "This is title", ...} 的字段,渲染出
⚠️ 注意事项:
总结:Go 模板的“包含”不是作用域继承,而是函数式调用——你传什么,子模板就拿到什么。养成 {{ template "xxx" . }} 的习惯,是避免变量丢失最简单、最可靠的实践。