亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

tmpl.Execute 和子文件 golang

tmpl.Execute 和子文件 golang

Go
喵喵時光機 2021-09-20 10:49:32
我需要幫助。我需要在子文件(,我的文本中的示例)中使用"html/template"的標記({{.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條經驗 獲得超6個贊

在core.tmpl你必須使用


{{template "article" .}}

如果.最后沒有指定,模板將與nil數據一起執行。指定.將傳遞.給調用模板的值。


引用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.


查看完整回答
反對 回復 2021-09-20
  • 1 回答
  • 0 關注
  • 306 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號