Provides helpers on top of html/template
to dynamically parse all templates from the specific directory and provides a unified interface for accessing them. In addition, the package provides the ability to use the Jinja2/Django like {{ extends }}
tag.
go get -u github.com/zero-pkg/tpl
tmpl := tpl.Must(tpl.New().ParseDir("templates", ".html"))
if err := tmpl.Execute(os.Stdout, "content.html", ""); err != nil {
panic(err)
}
The {{ extends }}
tag is the key here. It tells the package that this template “extends” another template.
When the package evaluates this template, it first locates the parent.
The extends tag should be the first tag in the template.
# parent.html
body: {{ block "content" . }}Hi from parent.{{ end }}
# child.html
{{ extends "parent.html" }}
{{ block "content" . }}Hi from child.{{ end }}
# grandchild.html
{{ extends "child.html" }}
{{ block "content" . }}Hi from grandchild.{{ end }}
Rendering grandchild.html
will give body: Hi from grandchild.