我想用 GOtemplate 生成 2 個單獨的文件,假設我有以下代碼:aBuffer := new(bytes.Buffer)bBuffer := new(bytes.Buffer)aTmpl, _ := template.ParseFiles(aFilePath)aTmpl.Execute(aBuffer, someVariables)bTmpl, _ := template.ParseFiles(bFilePath)bTmpl.Execute(bBuffer, someVariables)假設我對這兩個文件使用公共 var(不是來自“someVariables”golang var),我是否有辦法像 Helm 一樣在單獨的文件中聲明它?{{ define myVar }}the-var{{ end }}然后保留一種可以在 aTmpl 和 bTmpl 中重復使用的上下文:{{ template myVar .}}
1 回答

拉莫斯之舞
TA貢獻1820條經(jīng)驗 獲得超10個贊
使用通用定義創(chuàng)建第三個文件:
{{define "myVar"}}
the-var
{{end}}
與其他文件一起解析該文件:
aTmpl, _ := template.ParseFiles(aFilePath, commonFilePath)
bTmpl, _ := template.ParseFiles(bFilePath, commonFilePath)
在兩個模板中使用“myvar”,如下所示:
{{template "myVar" .}}
- 1 回答
- 0 關注
- 132 瀏覽
添加回答
舉報
0/150
提交
取消