custom: Allow override of home page

This commit is contained in:
Tom Wiesing 2023-01-08 13:53:53 +01:00
parent 729b9cfb51
commit cf59bd7db7
No known key found for this signature in database
4 changed files with 45 additions and 20 deletions

View file

@ -6,11 +6,13 @@
{{ end }} {{ end }}
{{ define "content" }} {{ define "content" }}
{{ block "@custom/about" . }}
<div class="pure-u-1"> <div class="pure-u-1">
<p> <p>
For more information, see <a href="{{ .SelfRedirect }}">{{ .SelfRedirect }}</a>. For more information, see <a href="{{ .SelfRedirect }}">{{ .SelfRedirect }}</a>.
</p> </p>
</div> </div>
{{ end }}
<div class="pure-u-1"> <div class="pure-u-1">
<h2>WissKIs on this Distillery</h2> <h2>WissKIs on this Distillery</h2>

View file

@ -11,8 +11,8 @@ func (custom *Custom) CustomAssetsPath() string {
return filepath.Join(custom.Config.DeployRoot, "core", "assets") return filepath.Join(custom.Config.DeployRoot, "core", "assets")
} }
func (custom *Custom) FooterPath() string { func (custom *Custom) CustomAssetPath(name string) string {
return filepath.Join(custom.CustomAssetsPath(), "footer.html") return filepath.Join(custom.CustomAssetsPath(), name)
} }
func (custom *Custom) BackupName() string { return "custom" } func (custom *Custom) BackupName() string { return "custom" }

View file

@ -8,35 +8,56 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
) )
const footerName = "footer" const (
footerName = "@custom/footer"
aboutName = "@custom/about"
)
//go:embed "footer.html" //go:embed "footer.html"
var defaultTemplateStr string var footerTemplateStr string
var defaultTemplate = template.Must(template.New("footer.html").Parse(defaultTemplateStr)) var defaultFooterTemplate = template.Must(template.New("footer.html").Parse(footerTemplateStr))
// Template creates a copy of template with shared template parts updated accordingly. // 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. // Any template using this should use one of the template contexts in this package.
func (custom *Custom) Template(tpl *template.Template) *template.Template { 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 // add all the fixed parse trees
template.Must(clone.AddParseTree(footerName, tree)) // add the parse tree to it 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 return clone // and return the tree
} }
// footerTemplate returns a new copy of the footer template // getTemplateAsset returns an overridable template asset.
func (custom *Custom) footerTemplate() *parse.Tree { //
footer, err := (func() (*template.Template, error) { // If the asset named can successfully be parsed, it is returned.
data, err := environment.ReadFile(custom.Environment, custom.FooterPath()) // 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 { if err != nil {
return nil, err return nil, err
} }
return template.New("footer.html").Parse(string(data)) return template.New(name).Parse(string(data))
})() })()
if err != nil { if err != nil {
return defaultTemplate.Tree.Copy() return nil
} }
return template.Tree
return footer.Tree
} }

View file

@ -21,8 +21,10 @@
</main> </main>
<footer> <footer>
{{ block "footer" .BaseContext }} {{ block "@custom/footer" .BaseContext }}
<!-- no footer --> <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 }} {{ end }}
</footer> </footer>