我有一些小的 .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
游樂場版本。
- 1 回答
- 0 關注
- 118 瀏覽
添加回答
舉報
0/150
提交
取消