Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84a9faff29 | ||
|
|
1c1878cfca | ||
|
|
c99b000b1d |
No files matched your search
@@ -42,7 +42,15 @@ type NavItem struct {
|
|||||||
// username/logout group).
|
// username/logout group).
|
||||||
type PageHeader struct {
|
type PageHeader struct {
|
||||||
Title string
|
Title string
|
||||||
Name string // lowercase product name, e.g. "yasta"; used in <title> and the footer
|
// AppName is the lowercase product name, e.g. "yasta", used in
|
||||||
|
// <title> and the footer. Deliberately not "Name" -- a consuming
|
||||||
|
// project's own per-page data struct commonly embeds both PageHeader
|
||||||
|
// and a domain type that has its own Name field (e.g. a Kubernetes
|
||||||
|
// resource's name); two same-depth promoted "Name" fields make
|
||||||
|
// html/template's ".Name" an ambiguous selector that only fails at
|
||||||
|
// execution time, not at parse time.
|
||||||
|
AppName string
|
||||||
|
Lang string // <html lang="...">; "" defaults to "en"
|
||||||
Brand string // navbar brand text, e.g. "YASTA"
|
Brand string // navbar brand text, e.g. "YASTA"
|
||||||
BrandInitial string // single-letter favicon glyph, e.g. "y"
|
BrandInitial string // single-letter favicon glyph, e.g. "y"
|
||||||
Subtitle string
|
Subtitle string
|
||||||
@@ -58,6 +66,16 @@ type PageHeader struct {
|
|||||||
NavbarRightExtra template.HTML // extra markup rendered after Username/Logout in the navbar's right group
|
NavbarRightExtra template.HTML // extra markup rendered after Username/Logout in the navbar's right group
|
||||||
ExtraHeadHTML template.HTML // extra <link>/<script> tags appended to <head>
|
ExtraHeadHTML template.HTML // extra <link>/<script> tags appended to <head>
|
||||||
ExtraBodyHTML template.HTML // extra <script> tags appended before </body>
|
ExtraBodyHTML template.HTML // extra <script> tags appended before </body>
|
||||||
|
// BodyAttrs/SidebarAttrs are raw extra attributes (include their own
|
||||||
|
// leading space, e.g. ` x-data="{ open: true }"`) spliced into
|
||||||
|
// <body...> and the sidebar's <nav...> tag -- an escape hatch for a
|
||||||
|
// consuming project's own client-side framework state (e.g. yakwui's
|
||||||
|
// Alpine.js collapsible sidebar), which the shared markup has no
|
||||||
|
// generic opinion about. template.HTMLAttr, not template.HTML --
|
||||||
|
// html/template's contextual autoescaper requires the attribute-list
|
||||||
|
// sentinel type in this position or it silently drops the value.
|
||||||
|
BodyAttrs template.HTMLAttr
|
||||||
|
SidebarAttrs template.HTMLAttr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renderer holds one independently parsed *template.Template per page:
|
// Renderer holds one independently parsed *template.Template per page:
|
||||||
|
|||||||
+32
-1
@@ -30,7 +30,7 @@ func TestNewRendererAndPage(t *testing.T) {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
data := PageHeader{
|
data := PageHeader{
|
||||||
Title: "Dashboard",
|
Title: "Dashboard",
|
||||||
Name: "example",
|
AppName: "example",
|
||||||
Brand: "EXAMPLE",
|
Brand: "EXAMPLE",
|
||||||
BrandInitial: "e",
|
BrandInitial: "e",
|
||||||
BasePath: "",
|
BasePath: "",
|
||||||
@@ -47,6 +47,37 @@ func TestNewRendererAndPage(t *testing.T) {
|
|||||||
`class="nav-link active"`,
|
`class="nav-link active"`,
|
||||||
"test-build",
|
"test-build",
|
||||||
"<title>Dashboard · example</title>",
|
"<title>Dashboard · example</title>",
|
||||||
|
`<html lang="en">`,
|
||||||
|
"<body>",
|
||||||
|
} {
|
||||||
|
if !strings.Contains(out, want) {
|
||||||
|
t.Errorf("rendered output missing %q\n---\n%s", want, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPageHeaderOverrides(t *testing.T) {
|
||||||
|
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewRenderer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
data := PageHeader{
|
||||||
|
Title: "Dashboard",
|
||||||
|
Lang: "de",
|
||||||
|
BodyAttrs: template.HTMLAttr(` x-data="{ open: true }"`),
|
||||||
|
SidebarAttrs: template.HTMLAttr(` :class="{ hidden: !open }"`),
|
||||||
|
}
|
||||||
|
if err := r.Page(&buf, "dashboard", data); err != nil {
|
||||||
|
t.Fatalf("Page: %v", err)
|
||||||
|
}
|
||||||
|
out := buf.String()
|
||||||
|
|
||||||
|
for _, want := range []string{
|
||||||
|
`<html lang="de">`,
|
||||||
|
`<body x-data="{ open: true }">`,
|
||||||
|
`sidebar d-md-block" :class="{ hidden: !open }">`,
|
||||||
} {
|
} {
|
||||||
if !strings.Contains(out, want) {
|
if !strings.Contains(out, want) {
|
||||||
t.Errorf("rendered output missing %q\n---\n%s", want, out)
|
t.Errorf("rendered output missing %q\n---\n%s", want, out)
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
{{define "base"}}<!doctype html>
|
{{define "base"}}<!doctype html>
|
||||||
<html lang="en">
|
<html lang="{{if .Lang}}{{.Lang}}{{else}}en{{end}}">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title>{{.Title}}{{if .Name}} · {{.Name}}{{end}}</title>
|
<title>{{.Title}}{{if .AppName}} · {{.AppName}}{{end}}</title>
|
||||||
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20100%20100'%3E%3Crect%20width='100'%20height='100'%20rx='20'%20fill='%2314171e'/%3E%3Ctext%20x='50'%20y='71'%20font-size='58'%20font-family='system-ui,sans-serif'%20font-weight='700'%20fill='%230b82b8'%20text-anchor='middle'%3E{{.BrandInitial}}%3C/text%3E%3C/svg%3E">
|
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20100%20100'%3E%3Crect%20width='100'%20height='100'%20rx='20'%20fill='%2314171e'/%3E%3Ctext%20x='50'%20y='71'%20font-size='58'%20font-family='system-ui,sans-serif'%20font-weight='700'%20fill='%230b82b8'%20text-anchor='middle'%3E{{.BrandInitial}}%3C/text%3E%3C/svg%3E">
|
||||||
<link rel="stylesheet" href="{{.BasePath}}/webui-static/bootstrap.min.css">
|
<link rel="stylesheet" href="{{.BasePath}}/webui-static/bootstrap.min.css">
|
||||||
<link rel="stylesheet" href="{{.BasePath}}/webui-static/style.css">
|
<link rel="stylesheet" href="{{.BasePath}}/webui-static/style.css">
|
||||||
<script src="{{.BasePath}}/webui-static/bootstrap.bundle.min.js" defer></script>
|
<script src="{{.BasePath}}/webui-static/bootstrap.bundle.min.js" defer></script>
|
||||||
{{.ExtraHeadHTML}}
|
{{.ExtraHeadHTML}}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body{{.BodyAttrs}}>
|
||||||
{{template "navbar" .}}
|
{{template "navbar" .}}
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<nav class="col-md-2 col-lg-2 sidebar d-md-block">
|
<nav class="col-md-2 col-lg-2 sidebar d-md-block"{{.SidebarAttrs}}>
|
||||||
{{template "sidebar" .}}
|
{{template "sidebar" .}}
|
||||||
</nav>
|
</nav>
|
||||||
<main class="col-md-10 col-lg-10 ms-sm-auto px-md-4 py-3">
|
<main class="col-md-10 col-lg-10 ms-sm-auto px-md-4 py-3">
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
{{define "footer"}}
|
{{define "footer"}}
|
||||||
<footer class="footer">{{.Name}} web UI · {{buildInfo}}</footer>
|
<footer class="footer">{{.AppName}} web UI · {{buildInfo}}</footer>
|
||||||
{{end}}
|
{{end}}
|
||||||
Reference in new issue
Block a user