templates: Share fragments

This commit is contained in:
Tom Wiesing 2022-10-20 11:26:35 +02:00
parent dd7be3f520
commit d64e6f8ec3
No known key found for this signature in database
12 changed files with 111 additions and 93 deletions

View file

@ -1,26 +1,25 @@
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WissKI Distillery</title>
{{ CSS }}
</head>
<body>
<header>
<h1>WissKI Distillery</h1>
</header>
<main>
<div class="pure-g">
<div class="pure-u-1">
{{ template "_base.html" . }}
{{ define "title" }}WissKI Distillery{{ end }}
{{ define "header/time" }}
<!-- no header/time -->
{{ end }}
{{ define "header"}}
<!-- no header -->
{{ end }}
{{ define "content" }}
<div class="pure-u-1">
<p>
For more information, see <a href="{{ .SelfRedirect }}">{{ .SelfRedirect }}</a>.
</p>
</div>
</div>
<div class="pure-u-1">
<div class="pure-u-1">
<h2>WissKIs on this Distillery</h2>
</div>
</div>
{{range .Instances}}
{{range .Instances}}
{{ if .Running }}
<div class="pure-u-1-3">
<h3>{{.Slug}}</h3>
@ -29,12 +28,5 @@
</p>
</div>
{{ end }}
{{ end }}
</main>
<footer>
Generated at <code>{{ .Time }}</code>
</footer>
{{ JS }}
</body>
{{ end }}
{{ end }}

View file

@ -51,7 +51,7 @@ func (home *Home) updateRender(ctx context.Context, io stream.IOStream) {
//go:embed "home.html"
var homeHTMLStr string
var homeTemplate = static.AssetsHomeHome.MustParse(nil, homeHTMLStr)
var homeTemplate = static.AssetsHomeHome.MustParseShared("home.html", homeHTMLStr)
func (home *Home) homeRender() ([]byte, error) {
var context HomeContext

View file

@ -1,29 +1,19 @@
package info
import (
_ "embed"
"html/template"
"net/http"
"time"
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
)
//go:embed "html/base.html"
var baseTemplateString string
var baseTemplate = template.Must(template.New("base.html").Parse(baseTemplateString))
func base(name string) *template.Template {
clone := template.Must(baseTemplate.Clone())
clone.Tree.Name = name
return clone
}
//go:embed "html/info_components.html"
var componentsTemplateString string
var componentsTemplate = static.AssetsComponentsIndex.MustParse(
base("info_components.html"),
var componentsTemplate = static.AssetsComponentsIndex.MustParseShared(
"info_components.html",
componentsTemplateString,
)

View file

@ -1,8 +1,6 @@
{{ template "_base.html" . }}
{{ define "title" }}Distillery Control Page - Components Page{{ end }}
{{ define "head" }}{{ CSS }}{{ end }}
{{ define "footer" }}{{ JS }}{{ end }}
{{ define "header"}}
<p>
<a class="pure-button" href="/dis/index">Control</a> &gt;

View file

@ -1,8 +1,6 @@
{{ template "_base.html" . }}
{{ define "title" }}Distillery Control Page{{ end }}
{{ define "head" }}{{ CSS }}{{ end }}
{{ define "footer" }}{{ JS }}{{ end }}
{{ define "header"}}
<p>
<a class="pure-button pure-button-primary" href="/dis/index">Control</a>

View file

@ -1,8 +1,6 @@
{{ template "_base.html" . }}
{{ define "title" }}Distillery Control Page - {{ .Info.Slug }}{{ end }}
{{ define "head" }}{{ CSS }}{{ end }}
{{ define "footer" }}{{ JS }}{{ end }}
{{ define "header"}}
<p>
<a class="pure-button" href="/dis/index">Control</a> &gt;

View file

@ -15,8 +15,8 @@ import (
//go:embed "html/info_index.html"
var indexTemplateStr string
var indexTemplate = static.AssetsControlIndex.MustParse(
base("info_index.html"),
var indexTemplate = static.AssetsControlIndex.MustParseShared(
"info_index.html",
indexTemplateStr,
)

View file

@ -15,8 +15,8 @@ import (
//go:embed "html/info_instance.html"
var instanceTemplateString string
var instanceTemplate = static.AssetsControlInstance.MustParse(
base("info_instance.html"),
var instanceTemplate = static.AssetsControlInstance.MustParseShared(
"info_instance.html",
instanceTemplateString,
)

View file

@ -1,7 +1,6 @@
package static
import (
"encoding/json"
"html/template"
)
@ -25,26 +24,25 @@ type Assets struct {
//go:generate node build.mjs HomeHome ComponentsIndex ControlIndex ControlInstance
// MustParse parses a new template from the given source
// and registers the Asset functions to it.
// See [Assets.RegisterFuncs].
// and calls [RegisterAssoc] on it.
func (assets *Assets) MustParse(t *template.Template, value string) *template.Template {
if t == nil {
t = template.New("")
}
return template.Must(assets.RegisterFuncs(t).Parse(value))
t = template.Must(t.Parse(value))
assets.RegisterAssoc(t)
return t
}
// RegisterFuncs registers three new template functions called "JS", "CSS" and "json".
//
// "JS" and "CSS" take no arguments, and return appropriate tags to be inserted into html.
// json takes a single argument of any type, and returns it's encoding as a string to be inserted into the page.
func (assets *Assets) RegisterFuncs(t *template.Template) *template.Template {
return t.Funcs(template.FuncMap{
"JS": func() template.HTML { return template.HTML(assets.Scripts) },
"CSS": func() template.HTML { return template.HTML(assets.Styles) },
"json": func(data any) (string, error) {
bytes, err := json.Marshal(data)
return string(bytes), err
},
})
// MustParseShared is like [MustParse], but creates a new SharedTemplate instead
func (assets *Assets) MustParseShared(name string, value string) *template.Template {
return assets.MustParse(NewSharedTemplate(name), value)
}
// RegisterAssoc registers two new associated templates with t.
//
// The template "scripts" will render all script tags required.
// The template "styles" will render all style tags required.
//
// If either template already exists, it will be overwritten.
func (assets *Assets) RegisterAssoc(t *template.Template) {
t.New("scripts").Parse(assets.Scripts)
t.New("styles").Parse(assets.Styles)
}

View file

@ -0,0 +1,30 @@
package static
import (
"embed"
"encoding/json"
"html/template"
)
//go:embed "templates/*.html"
var templates embed.FS
var (
shared *template.Template = template.Must(template.ParseFS(templates, "templates/*.html"))
)
// NewSharedTemplate creates a new template with the given name.
// It will be able to make use of shared templates as well as functions.
func NewSharedTemplate(name string) *template.Template {
new := template.New(name)
new.Funcs(template.FuncMap{
"json": func(data any) (string, error) {
bytes, err := json.Marshal(data)
return string(bytes), err
},
})
for _, template := range shared.Templates() {
new.AddParseTree(template.Tree.Name, template.Tree.Copy())
}
return new
}

View file

@ -3,14 +3,18 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ block "title" . }}Distillery Control Page{{ end }}</title>
{{ block "head" . }}head{{ end }}
<title>{{ block "block" . }}Distillery Control Page{{ end }}</title>
{{ block "styles" . }}styles{{ end }}
</head>
<body>
<header>
<h1 id="top">{{ template "title" . }}</h1>
{{ block "header/time" . }}
{{ if .Time }}
<small>Generated at <code class="date">{{ .Time.Format "2006-01-02T15:04:05Z07:00" }}</code></small>
{{ end }}
{{ end }}
{{ block "header" . }}header{{ end }}
</header>
<main>
@ -19,8 +23,11 @@
</div>
</main>
{{ if .Time }}
<footer>
Generated at <code>{{ .Time }}</code>
</footer>
{{ block "footer" . }}footer{{ end }}
{{ end }}
{{ block "scripts" . }}scripts{{ end }}
</body>

View file

@ -7,7 +7,9 @@ import (
type HTMLHandler[T any] struct {
Handler func(r *http.Request) (T, error)
Template *template.Template // called with T
TemplateName string // name of template to render, defaults to root
}
var htmlInternalServerErr = []byte(`<!DOCTYPE HTML><title>Internal Server Error</title>Internal Server Error`)
@ -37,5 +39,10 @@ func (h HTMLHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// write out the response as json
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if h.TemplateName != "" {
h.Template.ExecuteTemplate(w, h.TemplateName, result)
} else {
h.Template.Execute(w, result)
}
}