为了账号安全,请及时绑定邮箱和手机立即绑定

带字符串的模板继承

带字符串的模板继承

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 关注
  • 68 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信