Skip to content

Instantly share code, notes, and snippets.

@blinkinglight
Last active February 1, 2024 05:13
Show Gist options
  • Save blinkinglight/b0b099cea4fc6c4f43a1daee8363a6bc to your computer and use it in GitHub Desktop.
Save blinkinglight/b0b099cea4fc6c4f43a1daee8363a6bc to your computer and use it in GitHub Desktop.
{{ define "htmlbody" }}
<h1>Page 1</h1>
{{ range .Items }}
{{ block "item-row" . }}
<div>
your page content {{ . }}
</div>
{{ end }}
{{ end }}
{{ end }}
//go:embed templates/*.html templates/*/*.html
var templates embed.FS
// <..>
func PageTemplate(name string) *template.Template {
templs := []string{name, "index.html"}
tpl, err := template.New(name).Funcs(/* if any */).ParseFS(templates, templs...)
return template.Must(tpl, err)
}
type VD struct {
Data map[string]interface{}
}
var vd = new(VD);
vd.Data["Items"] = []string{"one","two"}
PageTemplate("calendar.html").ExecuteTemplate(w, "index.html", vd)
// or
PageTemplate("calendar.html").ExecuteTemplate(w, "item-row", "one line")
<html>
<body>
<div class="content">
{{ template "htmlbody" . }}
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment