diff options
author | Florian Klink <flokli@flokli.de> | 2019-11-27T10·23+0100 |
---|---|---|
committer | Florian Klink <flokli@flokli.de> | 2019-11-27T10·24+0100 |
commit | 97eb1fae387dcfa9ceb121167aa977d430c11dde (patch) | |
tree | 0598e12c75351bafbe674c8cc000e46fdd1d135c | |
parent | 6bb7689a7b606051acc008e1e312f79974bd0859 (diff) |
loadTemplate: support loading mulitple templates
-rw-r--r-- | frontend/frontend.go | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/frontend/frontend.go b/frontend/frontend.go index ff7ace835bb8..c49b00517581 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -1,6 +1,7 @@ package frontend import ( + "fmt" "io/ioutil" "net/http" @@ -16,23 +17,34 @@ import ( //TODO: log last update -//loadTemplate loads a single template from statikFS and returns a template object -func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) { - tmpl := template.New(templateName).Funcs(funcMap) - statikFS, err := fs.New() - if err != nil { - return nil, err +//loadTemplate loads a list of templates, relative to the statikFS root, and a FuncMap, and returns a template object +func loadTemplate(templateNames []string, funcMap template.FuncMap) (*template.Template, error) { + if len(templateNames) == 0 { + return nil, fmt.Errorf("templateNames can't be empty") } - r, err := statikFS.Open("/" + templateName) + tmpl := template.New(templateNames[0]).Funcs(funcMap) + statikFS, err := fs.New() if err != nil { return nil, err } - defer r.Close() - contents, err := ioutil.ReadAll(r) - if err != nil { - return nil, err + + for _, templateName := range templateNames { + 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 + } } - return tmpl.Parse(string(contents)) + + return tmpl, nil } // MakeFrontend configures the router and returns a new Frontend struct |