loadTemplate: support loading mulitple templates

This commit is contained in:
Florian Klink 2019-11-27 11:23:12 +01:00
parent 6bb7689a7b
commit 97eb1fae38

View file

@ -1,6 +1,7 @@
package frontend package frontend
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -16,23 +17,34 @@ import (
//TODO: log last update //TODO: log last update
//loadTemplate loads a single template from statikFS and returns a template object //loadTemplate loads a list of templates, relative to the statikFS root, and a FuncMap, and returns a template object
func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) { func loadTemplate(templateNames []string, funcMap template.FuncMap) (*template.Template, error) {
tmpl := template.New(templateName).Funcs(funcMap) if len(templateNames) == 0 {
return nil, fmt.Errorf("templateNames can't be empty")
}
tmpl := template.New(templateNames[0]).Funcs(funcMap)
statikFS, err := fs.New() statikFS, err := fs.New()
if err != nil { if err != nil {
return nil, err return nil, err
} }
r, err := statikFS.Open("/" + templateName)
if err != nil { for _, templateName := range templateNames {
return nil, err r, err := statikFS.Open("/" + templateName)
if err != nil {
return nil, err
}
defer r.Close()
contents, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
tmpl, err = tmpl.Parse(string(contents))
if err != nil {
return nil, err
}
} }
defer r.Close()
contents, err := ioutil.ReadAll(r) return tmpl, nil
if err != nil {
return nil, err
}
return tmpl.Parse(string(contents))
} }
// MakeFrontend configures the router and returns a new Frontend struct // MakeFrontend configures the router and returns a new Frontend struct