Is it possible to have nested templates in Go using the standard library?

前端 未结 5 1724
时光说笑
时光说笑 2020-12-12 09:30

How do I get nested templates like Jinja has in the python runtime. TBC what I mean is how do I have a bunch of templates inherit from a base templates, just filing in block

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 10:07

    Yes it is possible. A html.Template is actually a set of template files. If you execute a defined block in this set, it has access to all the other blocks defined in this set.

    If you create a map of such template sets on your own, you have basically the same flexibility that Jinja / Django offers. The only difference is that the html/template package has no direct access to the file system, so you have to parse and compose the templates on your own.

    Consider the following example with two different pages ("index.html" and "other.html") that both inherit from "base.html":

    // Content of base.html:
    {{define "base"}}
      {{template "head" .}}
      {{template "body" .}}
    {{end}}
    
    // Content of index.html:
    {{define "head"}}index{{end}}
    {{define "body"}}index{{end}}
    
    // Content of other.html:
    {{define "head"}}other{{end}}
    {{define "body"}}other{{end}}
    

    And the following map of template sets:

    tmpl := make(map[string]*template.Template)
    tmpl["index.html"] = template.Must(template.ParseFiles("index.html", "base.html"))
    tmpl["other.html"] = template.Must(template.ParseFiles("other.html", "base.html"))
    

    You can now render your "index.html" page by calling

    tmpl["index.html"].Execute("base", data)
    

    and you can render your "other.html" page by calling

    tmpl["other.html"].Execute("base", data)
    

    With some tricks (e.g. a consistent naming convention of your template files), it's even possible to generate the tmpl map automatically.

提交回复
热议问题