Refactor html templates

This commit entirely refactors the use of html templates. Instead of
inheriting from a shared template, we insert the results into a base
template.
This commit is contained in:
Tom Wiesing 2023-01-20 14:42:37 +01:00
parent 6ede99d7c6
commit d235ee4e5c
No known key found for this signature in database
59 changed files with 869 additions and 777 deletions

View file

@ -0,0 +1,5 @@
<div class="pure-u-1">
<p>
For more information, see <a href="{{ .SelfRedirect }}">{{ .SelfRedirect }}</a>.
</p>
</div>

View file

@ -7,7 +7,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
)
@ -15,7 +15,7 @@ import (
type Home struct {
component.Base
Dependencies struct {
Templating *templates.Templating
Templating *templating.Templating
Instances *instances.Instances
}

View file

@ -3,41 +3,73 @@ package home
import (
"context"
_ "embed"
"html/template"
"net/http"
"strings"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
)
//go:embed "public.html"
var publicHTML []byte
var publicTemplate = templates.Parse[publicContext]("public.html", publicHTML, assets.AssetsDefault)
var publicTemplate = templating.Parse[publicContext](
"public.html", publicHTML, nil,
type publicContext struct {
templates.BaseContext
templating.Title("WissKI Distillery"),
templating.Assets(assets.AssetsDefault),
)
//go:embed "about.html"
var aboutHTML string
var aboutTemplate = template.Must(template.New("about.html").Parse(aboutHTML))
// aboutContext is passed to about.html
type aboutContext struct {
Instances []status.WissKI
SelfRedirect string
}
// publicCOntext is passed to public.html
type publicContext struct {
templating.RuntimeFlags
aboutContext
About template.HTML
}
func (home *Home) publicHandler(ctx context.Context) http.Handler {
tpl := publicTemplate.Prepare(home.Dependencies.Templating, templates.BaseContextGaps{
Crumbs: []component.MenuItem{
{Title: "WissKI Distillery", Path: "/"},
},
})
tpl := publicTemplate.Prepare(
home.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "WissKI Distillery", Path: "/"},
),
)
about := home.Dependencies.Templating.GetCustomizable(aboutTemplate)
return tpl.HTMLHandler(func(r *http.Request) (pc publicContext, err error) {
// only act on the root path!
if strings.TrimSuffix(r.URL.Path, "/") != "" {
return pc, httpx.ErrNotFound
}
pc.Instances = home.homeInstances.Get(nil)
pc.SelfRedirect = home.Config.SelfRedirect.String()
// prepare about
pc.aboutContext.Instances = home.homeInstances.Get(nil)
pc.aboutContext.SelfRedirect = home.Config.SelfRedirect.String()
// render the about template
var builder strings.Builder
if err := about.Execute(&builder, pc.aboutContext); err != nil {
return pc, nil
}
// and return about!
pc.About = template.HTML(builder.String())
return
})

View file

@ -1,14 +1,4 @@
{{ template "_base.html" . }}
{{ define "title" }}WissKI Distillery{{ end }}
{{ define "content" }}
{{ block "@custom/about" . }}
<div class="pure-u-1">
<p>
For more information, see <a href="{{ .SelfRedirect }}">{{ .SelfRedirect }}</a>.
</p>
</div>
{{ end }}
{{ .About }}
<div class="pure-u-1">
<h2>WissKIs on this Distillery</h2>
@ -33,4 +23,3 @@
</div>
{{ end }}
{{ end }}
{{ end }}