
在 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", ...} 的字段,渲染出
⚠️ 注意事项:
- {{ template "name" }} 等价于 {{ template "name" nil }},即显式传入 nil 上下文;
- 若需传入其他结构体或字段(如 {{ template "header" .User }}),也可灵活指定;
- define 定义的模板本身不接收隐式上下文,其数据完全依赖 template 调用时传入的第二个参数;
- 使用 {{ with .Something }}...{{ end }} 等控制结构时,作用域会临时切换,但 template 调用仍需显式传参。
总结:Go 模板的“包含”不是作用域继承,而是函数式调用——你传什么,子模板就拿到什么。养成 {{ template "xxx" . }} 的习惯,是避免变量丢失最简单、最可靠的实践。









