在golang模板之间共享变量

问题描述 投票:0回答:1

看来这个问题应该被编辑

go go-templates
1个回答
3
投票

使用通用定义创建第三个文件:

{{define "myVar"}}
the-var
{{end}}

与其他文件一起解析该文件:

aTmpl, _ := template.ParseFiles(aFilePath, commonFilePath)

bTmpl, _ := template.ParseFiles(bFilePath, commonFilePath)

在两个模板中使用“myvar”,如下:

{{template "myVar" .}}
© www.soinside.com 2019 - 2024. All rights reserved.