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

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

帶字符串的模板繼承

帶字符串的模板繼承

Go
青春有我 2023-06-05 19:27:03
我有一些小的 .html 文件,我解析如下:template.Must(    template.New("test").        ParseFiles(            "templates/f1.html",            "templates/f2.html",            "templates/f3.html",        ),)這些都是非常小的文件,例如,我想讓這些只是字符串以使我的代碼更容易,但是我如何使用字符串進行相同類型的模板繼承?我所看到的只是 Parse 方法,但它只需要 1 個字符串:func (t *Template) Parse(text string) (*Template, error)我的字符串:f1 := `Hi there {{template "content" .}}`f2 := `{{define "content"}}bob {{template "content2" .}}{{end}}`f3 := `{{define "content2"}}ross{{end}}`不確定如何讓這些“一起玩”。(這是我正在做的事情的一個簡化示例,這些字符串在多個地方使用,因此將它們全部組合為 1 沒有意義)。
查看完整描述

1 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

嘗試:


const (

    f1 = `Hi there {{template "content" .}}`

    f2 = `{{define "content"}}bob {{template "content2" .}}{{end}}`

    f3 = `{{define "content2"}}ross{{end}}`

)


func main() {

    t := template.New("test")


    t.Parse(f1)

    t.Parse(f2)

    t.Parse(f3)


    t.Execute(os.Stdout, nil)

}

產生: Hi there bob ross

游樂場版本。



查看完整回答
反對 回復 2023-06-05
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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