You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Golang模板变量FooterField在footer模板中无法正常解析问题

问题原因

Go标准库html/template调用子模板时不会默认传递当前上下文数据,必须显式指定传入参数:

  • 你在index.html中调用header模板时使用{{template "header" . }},末尾的.代表将当前的Data结构体实例传递给header子模板,因此header可以正常读取到所有字段值
  • 但调用footer模板时使用的是{{template "footer"}},没有传入任何参数,footer模板的作用域内没有对应的数据对象,因此.FooterField取值为空。
修复方案

修改index.html中调用footer模板的代码,补充传入当前上下文参数即可:

{{define "index"}}{{template "header" . }}
<h1>Index template</h1>
<div>{{.FooterField}}</div>
{{template "footer" . }}{{end}}

修改后footer模板就能正常读取到FooterField的值。

内容的提问来源于stack exchange,提问作者pigfox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 06:15:03