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

@ -11,7 +11,7 @@ import (
"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/rs/zerolog"
"github.com/yuin/goldmark"
gmmeta "github.com/yuin/goldmark-meta"
@ -22,7 +22,7 @@ import (
type News struct {
component.Base
Dependencies struct {
Templating *templates.Templating
Templating *templating.Templating
}
}
@ -113,20 +113,26 @@ func Items() ([]Item, error) {
//go:embed "news.html"
var newsHTML []byte
var newsTemplate = templates.Parse[newsContext]("news.html", newsHTML, assets.AssetsDefault)
var newsTemplate = templating.Parse[newsContext](
"news.html", newsHTML, nil,
templating.Title("News"),
templating.Assets(assets.AssetsDefault),
)
type newsContext struct {
templates.BaseContext
templating.RuntimeFlags
Items []Item
}
// HandleRoute returns the handler for the requested path
func (news *News) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
tpl := newsTemplate.Prepare(news.Dependencies.Templating, templates.BaseContextGaps{
Crumbs: []component.MenuItem{
{Title: "News", Path: "/news/"},
},
})
tpl := newsTemplate.Prepare(
news.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "News", Path: "/news/"},
),
)
items, itemsErr := Items()
if itemsErr != nil {

View file

@ -1,8 +1,3 @@
{{ template "_base.html" . }}
{{ define "title" }}News{{ end }}
{{ define "content" }}
<div class="pure-u-1">
This page contains news items from the distillery.
</div>
@ -14,5 +9,3 @@
{{ .Content }}
</div>
{{ end }}
{{ end }}