package templ import ( "embed" "html/template" "sync" ) //go:embed *.tpl var fs embed.FS var templates = make(map[string]*template.Template) var muTpl sync.RWMutex var baseTpl *template.Template func init() { baseTpl = template.Must(template.ParseFS(fs, "base.tpl")) } func Lookup(name string) *template.Template { muTpl.RLock() tpl := templates[name] muTpl.RUnlock() if tpl == nil { return parse(name) } return tpl } func parse(name string) *template.Template { muTpl.Lock() defer muTpl.Unlock() if tpl := templates[name]; tpl != nil { // might've been created by another goroutine return tpl } b := template.Must(baseTpl.Clone()) t := template.Must(b.ParseFS(fs, name+".tpl")) templates[name] = t return t }