带字符串的模板继承

我有一些小的 .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 没有意义)。


青春有我
浏览 71回答 1
1回答

大话西游666

尝试: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游乐场版本。
打开App,查看更多内容
随时随地看视频慕课网APP