Add support for custom footer

This commit is contained in:
Tom Wiesing 2023-01-08 08:13:47 +01:00
parent 9f3e7a7b86
commit a292c25f84
No known key found for this signature in database
7 changed files with 60 additions and 3 deletions

View file

@ -3,6 +3,9 @@ package custom
import (
_ "embed"
"html/template"
"text/template/parse"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
)
const footerName = "footer"
@ -14,9 +17,26 @@ var defaultTemplate = template.Must(template.New("footer.html").Parse(defaultTem
// Template creates a copy of template with shared template parts updated accordingly.
// Any template using this should use one of the template contexts in this package.
func (custom *Custom) Template(tpl *template.Template) *template.Template {
tree := defaultTemplate.Tree.Copy()
tree := custom.footerTemplate()
clone := template.Must(tpl.Clone()) // create a clone of the template
template.Must(clone.AddParseTree(footerName, tree)) // add the parse tree to it
return clone // and return the tree
}
// footerTemplate returns a new copy of the footer template
func (custom *Custom) footerTemplate() *parse.Tree {
footer, err := (func() (*template.Template, error) {
data, err := environment.ReadFile(custom.Environment, custom.FooterPath())
if err != nil {
return nil, err
}
return template.New("footer.html").Parse(string(data))
})()
if err != nil {
return defaultTemplate.Tree.Copy()
}
return footer.Tree
}