~bigbes/sr-ht-ecore

ref: 3bd158fbb23241df31897f9ba47afac832ee2e1d sr-ht-ecore/chrome/templates.go -rw-r--r-- 1.1 KiB
3bd158fb — Eugene Blikh mcphttp: pin Flush with a flush that precedes any write 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package chrome

import (
	"embed"
	"html/template"
)

//go:embed templates/chrome.tmpl
var templateFS embed.FS

// Attach parses the shared chrome partials ("srht-nav", "srht-sections",
// "srht-env-banner", "srht-head-links", "srht-repo-list", "srht-repo-table")
// into t, so a service layout can invoke them. Call it once per template set,
// before the layout that references the partials is executed.
//
// It installs Funcs first, because the partials call them: an unknown function
// is a parse error, so a caller who had not merged Funcs would get a startup
// panic naming a template it never wrote. Installing them here also means the
// partials keep working when a service overrides a helper afterwards — its own
// Funcs call wins for its own templates.
func Attach(t *template.Template) (*template.Template, error) {
	return t.Funcs(Funcs()).ParseFS(templateFS, "templates/chrome.tmpl")
}

// MustAttach is Attach for the common wire-up-at-startup path, where a parse
// failure is a programmer error.
func MustAttach(t *template.Template) *template.Template {
	t, err := Attach(t)
	if err != nil {
		panic(err)
	}
	return t
}