custom: Add new footer template and context

This commit is contained in:
Tom Wiesing 2023-01-06 19:56:13 +01:00
parent 009d649ea6
commit bda763725e
No known key found for this signature in database
18 changed files with 197 additions and 33 deletions

View file

@ -0,0 +1,8 @@
package custom
import "github.com/FAU-CDI/wisski-distillery/internal/dis/component"
// Custom implements theme and page customization.
type Custom struct {
component.Base
}

View file

@ -0,0 +1,82 @@
package custom
import (
"html/template"
"net/http"
"reflect"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/tkw1536/goprogram/lib/reflectx"
)
const footerName = "footer"
// defaultTemplate is the default footer template
var defaultTemplate = template.Must(template.New("footer.html").Parse(`<p>Powered By WissKI Distillery</p>`))
// 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()
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
}
// NewContext returns a new BaseContext
func (custom *Custom) New() (ctx BaseContext) {
ctx.Use(custom.Base)
return
}
// NewForm is like New, but returns a new BaseFormContext
func (custom *Custom) NewForm(context httpx.FormContext) (ctx BaseFormContext) {
ctx.FormContext = context
ctx.Use(custom.Base)
return
}
// RenderContext can be used as httpx.Form.RenderTemplateContext.
// It returns a new [BaseFormContext].
func (custom *Custom) RenderContext(ctx httpx.FormContext, r *http.Request) any {
return BaseFormContext{
FormContext: ctx,
BaseContext: custom.New(),
}
}
// Update updates an embedded BaseContext field in context.
//
// Assumes that context is a pointer to a struct type.
// If this is not the case, might call panic().
func (custom *Custom) Update(context any) *BaseContext {
ctx := reflect.ValueOf(context).
Elem().FieldByName(contextName).Addr().
Interface().(*BaseContext)
ctx.Use(custom.Base)
return ctx
}
// contextName is the name of the [BaseContext] field.
var contextName = reflectx.TypeOf[BaseContext]().Name()
// BaseContext is a context struct shared by all contexts
type BaseContext struct {
Time time.Time // time this page was generated at
}
// Use updates this context to use the values from the given base.
// For convenience the passed context is also returned.
func (tc *BaseContext) Use(base component.Base) *BaseContext {
tc.Time = time.Now().UTC()
return tc
}
// BaseFormContext combines BaseContext and FormContext
type BaseFormContext struct {
BaseContext
httpx.FormContext
}

View file

@ -2,7 +2,7 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<meta charset="utf-8">
<title>{{ block "block" . }}WissKI Distillery{{ end }}</title>
{{ block "styles" . }}styles{{ end }}
@ -24,11 +24,15 @@
</div>
</main>
{{ if .Time }}
<footer>
Generated at <code>{{ .Time }}</code>
{{ if .Time }}
Generated at <code>{{ .Time }}</code>
{{ end }}
{{ block "footer" . }}
<!-- no footer by default -->
{{ end }}
</footer>
{{ end }}
{{ block "scripts" . }}scripts{{ end }}
</body>