如何解决golang的模板嵌套变量覆盖问题
1. 运行环境
go version go1.19.1 darwin/arm64
2. 问题描述?
使用golang的html/template
包,程序启动时通过tempate.ParseFS
来加载模板文件
现在有layouts文件夹存放全局模板,includes里存放存放单独页面
layouts/app.html
{{define "app"}}
{{block "content" .}}
{{end}}
{{end}}
includes/a.html
{{template "app" .}}
{{define "content"}}
list content
{{end}}
includes/b.html
{{template "app" .}}
{{define "content"}}
detail content
{{end}}
由于template这个包加载顺序的问题,后加载的b页面的content会覆盖a页面的content内容,渲染a页面时调用的是b页面的content逻辑导致渲染出错
3. 您期望得到的结果?
想知道大家有没有碰到这个问题
4. 您实际得到的结果?
b页面可以正常渲染,a页面会报错