我需要幫助。我需要在子文件(,我的文本中的示例)中使用"html/template"的標(biāo)記({{.Title}},示例"article.html"):// ...type Page struct { Test string}type News struct { Page Title string}func main() { t, _ := template.ParseFiles(filepath+"core.tmpl", filepath+"article.tmpl") p := &News{ Title: "TITLE", Page: Page{ Test: "TITLE", }, } t.Execute(wr, p)}代碼core.tmpl:{{template "article"}}代碼article.tmpl:{{define "article"}}{{.Title}}<br><br>{{.Page.Test}}{{end}}
1 回答

慕勒3428872
TA貢獻1848條經(jīng)驗 獲得超6個贊
在core.tmpl你必須使用
{{template "article" .}}
如果.最后沒有指定,模板將與nil數(shù)據(jù)一起執(zhí)行。指定.將傳遞.給調(diào)用模板的值。
引用text/template包文檔,Actions部分:
{{template "name"}}
The template with the specified name is executed with nil data.
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
- 1 回答
- 0 關(guān)注
- 299 瀏覽
添加回答
舉報
0/150
提交
取消