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,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,26 +0,0 @@
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ block "title" . }}Distillery Control Page{{ end }}</title>
{{ block "head" . }}head{{ end }}
</head>
<body>
<header>
<h1 id="top">{{ template "title" . }}</h1>
<small>Generated at <code class="date">{{ .Time.Format "2006-01-02T15:04:05Z07:00" }}</code></small>
{{ block "header" . }}header{{ end }}
</header>
<main>
<div class="pure-g">
{{ block "content" . }}content{{ end }}
</div>
</main>
<footer>
Generated at <code>{{ .Time }}</code>
</footer>
{{ block "footer" . }}footer{{ end }}
</body>

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,
)