
在go模板中,`{{template "name"}}`默认不传递数据,导致被调用的子模板(如header.html)无法访问父模板的上下文变量(如`.title`);正确做法是显式传入当前上下文:`{{template "header.html" .}}`。
当你在主模板 post.html 中使用 {{template "header.html"}} 时,Go 的 html/template 包不会自动将当前执行上下文(即 page 结构体)传递给被嵌入的子模板。这意味着 header.html 中的 {{.Title}} 实际上是在一个空上下文(nil 或未初始化结构体)中求值,因此渲染为空字符串。
✅ 正确写法是在 post.html 中显式传入当前数据:
{{template "header.html" .}}
{{.Body}}
{{template "footer.html" .}}注意末尾的 . —— 它代表当前模板执行时的数据上下文(即你传入 t.Execute(f, page) 的 Page 实例)。这样 header.html 就能正常访问 {{.Title}} 和 {{.Body}} 等字段。
? 补充说明:
- {{template "name"}}:无参调用,子模板接收 nil 上下文;
- {{template "name" .}}:将当前上下文传入;
- {{template "name" $}}:效果同上($ 指向最外层模板的数据);
- {{template "name" .Page}}:可传入嵌套字段,适用于更复杂的数据结构。
⚠️ 注意事项:
- 所有被 {{template}} 引用的子模板(如 header.html、footer.html)必须已通过 ParseGlob 或 ParseFiles 加载到同一 *template.Template 实例中;
- 若子模板中意外引用了不存在的字段(如 {{.NonExistent}}),默认静默失败(输出空字符串),建议开发期启用 template.Must() 并配合日志验证数据流;
- 避免在子模板中修改传入的结构体指针(Go 模板不支持副作用),所有数据应通过顶层 Execute 一次性注入。
通过这一微小但关键的语法调整,你的 index.html 即可正确渲染为:
I'm the title
And I'm the body
这是 Go 模板“显式数据流”设计哲学的典型体现:没有隐式继承,只有显式传递——既保障了模板行为的可预测性,也避免了作用域污染问题。










