Add PageHeader.Lang, BodyAttrs, SidebarAttrs escape hatches
Lang: <html lang> override (defaults to "en") for a non-English UI. BodyAttrs/SidebarAttrs: raw extra attributes spliced into <body> and the sidebar's <nav> tag, typed template.HTMLAttr (not template.HTML -- the contextual autoescaper requires the attribute-list sentinel type in this position or it silently drops the value). Needed by yakwui's Alpine.js-driven collapsible mobile sidebar, which has no other way to carry x-data state onto a common ancestor of the toggle button and the sidebar nav.
This commit is contained in:
1 parent
c99b000b1d
commit
1c1878cfca
3 files changed
+38
-4
No files matched your search
@@ -43,6 +43,7 @@ type NavItem struct {
|
|||||||
type PageHeader struct {
|
type PageHeader struct {
|
||||||
Title string
|
Title string
|
||||||
Name string // lowercase product name, e.g. "yasta"; used in <title> and the footer
|
Name string // lowercase product name, e.g. "yasta"; used in <title> and the footer
|
||||||
|
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
|
||||||
@@ -63,9 +64,11 @@ type PageHeader struct {
|
|||||||
// <body...> and the sidebar's <nav...> tag -- an escape hatch for a
|
// <body...> and the sidebar's <nav...> tag -- an escape hatch for a
|
||||||
// consuming project's own client-side framework state (e.g. yakwui's
|
// consuming project's own client-side framework state (e.g. yakwui's
|
||||||
// Alpine.js collapsible sidebar), which the shared markup has no
|
// Alpine.js collapsible sidebar), which the shared markup has no
|
||||||
// generic opinion about.
|
// generic opinion about. template.HTMLAttr, not template.HTML --
|
||||||
BodyAttrs template.HTML
|
// html/template's contextual autoescaper requires the attribute-list
|
||||||
SidebarAttrs template.HTML
|
// 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:
|
||||||
|
|||||||
@@ -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,5 +1,5 @@
|
|||||||
{{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">
|
||||||
|
|||||||
Reference in new issue
Block a user