mirror of
https://github.com/mainnika/nikita-tokarch-uk.git
synced 2026-05-25 01:03:35 +00:00
Use constructor-like function to load templates
This commit is contained in:
@@ -24,14 +24,14 @@ var content embed.FS
|
||||
var Templates *template.Template
|
||||
|
||||
// Load embeded templates
|
||||
func init() {
|
||||
func Load() (err error) {
|
||||
|
||||
Templates = template.New("")
|
||||
Templates.Funcs(UseFuncs())
|
||||
|
||||
tmplNames, err := fs.Glob(content, "*.go.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return fmt.Errorf("cannot match templates names using glob, %w", err)
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
@@ -42,21 +42,23 @@ func init() {
|
||||
|
||||
tmplContent, err := content.Open(name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return fmt.Errorf("cannot open template content, name:%s, %w", name, err)
|
||||
}
|
||||
size, err := buf.ReadFrom(tmplContent)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return fmt.Errorf("cannot read template content, name:%s, %w", name, err)
|
||||
}
|
||||
tmpl, err := Templates.New(name).Parse(buf.String())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return fmt.Errorf("cannot parse template, name:%s, %w", name, err)
|
||||
}
|
||||
|
||||
logrus.Debugf("Found template: %s, size:%d", tmpl.Name(), size)
|
||||
}
|
||||
|
||||
logrus.Debugf("Templates loading complete%s", Templates.DefinedTemplates())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// MustLookup wraps lookup function for the root template namespace
|
||||
|
||||
Reference in New Issue
Block a user