custom: Allow override of home page
This commit is contained in:
parent
729b9cfb51
commit
cf59bd7db7
4 changed files with 45 additions and 20 deletions
|
|
@ -11,8 +11,8 @@ func (custom *Custom) CustomAssetsPath() string {
|
|||
return filepath.Join(custom.Config.DeployRoot, "core", "assets")
|
||||
}
|
||||
|
||||
func (custom *Custom) FooterPath() string {
|
||||
return filepath.Join(custom.CustomAssetsPath(), "footer.html")
|
||||
func (custom *Custom) CustomAssetPath(name string) string {
|
||||
return filepath.Join(custom.CustomAssetsPath(), name)
|
||||
}
|
||||
|
||||
func (custom *Custom) BackupName() string { return "custom" }
|
||||
|
|
|
|||
|
|
@ -8,35 +8,56 @@ import (
|
|||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
const footerName = "footer"
|
||||
const (
|
||||
footerName = "@custom/footer"
|
||||
aboutName = "@custom/about"
|
||||
)
|
||||
|
||||
//go:embed "footer.html"
|
||||
var defaultTemplateStr string
|
||||
var defaultTemplate = template.Must(template.New("footer.html").Parse(defaultTemplateStr))
|
||||
var footerTemplateStr string
|
||||
var defaultFooterTemplate = template.Must(template.New("footer.html").Parse(footerTemplateStr))
|
||||
|
||||
// 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 := custom.footerTemplate()
|
||||
// create a clone of the template
|
||||
clone := template.Must(tpl.Clone())
|
||||
|
||||
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
|
||||
// add all the fixed parse trees
|
||||
footerTree := custom.getTemplateAsset(defaultFooterTemplate)
|
||||
template.Must(clone.AddParseTree(footerName, footerTree))
|
||||
|
||||
// optionally add the about asset
|
||||
if aboutTree := custom.readTemplateAsset("about.html"); clone.Lookup(aboutName) != nil && aboutTree != nil {
|
||||
template.Must(clone.AddParseTree(aboutName, aboutTree))
|
||||
}
|
||||
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())
|
||||
// getTemplateAsset returns an overridable template asset.
|
||||
//
|
||||
// If the asset named can successfully be parsed, it is returned.
|
||||
// If it can not be parsed, the default template is returned.
|
||||
func (custom *Custom) getTemplateAsset(dflt *template.Template) *parse.Tree {
|
||||
tree := custom.readTemplateAsset(dflt.Name())
|
||||
if tree == nil {
|
||||
return dflt.Tree.Copy()
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// readTemplateAsset is like getTemplateAssets, but takes an explicit name to read.
|
||||
// when the asset does not exist, or cannot be opened, returns nil.
|
||||
func (custom *Custom) readTemplateAsset(name string) *parse.Tree {
|
||||
template, err := (func() (*template.Template, error) {
|
||||
data, err := environment.ReadFile(custom.Environment, custom.CustomAssetPath(name))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return template.New("footer.html").Parse(string(data))
|
||||
return template.New(name).Parse(string(data))
|
||||
})()
|
||||
|
||||
if err != nil {
|
||||
return defaultTemplate.Tree.Copy()
|
||||
return nil
|
||||
}
|
||||
|
||||
return footer.Tree
|
||||
return template.Tree
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,10 @@
|
|||
</main>
|
||||
|
||||
<footer>
|
||||
{{ block "footer" .BaseContext }}
|
||||
<!-- no footer -->
|
||||
{{ block "@custom/footer" .BaseContext }}
|
||||
<div style="z-index:10000;position:fixed;top:0;left:0;width:100vh;height:100vw;background:red;text-align:center;padding:10vh 10vw;font-size:xx-large;font-weight:bold">
|
||||
<code>.Custom.Template()</code> not called
|
||||
</div>
|
||||
{{ end }}
|
||||
</footer>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue