Compare commits

...
5 Commits
Author SHA1 Message Date
terminar 1c1878cfca 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.
2026-09-01 09:40:26 +02:00
terminar c99b000b1d Add PageHeader.BodyAttrs/SidebarAttrs escape hatches
Needed by yakwui's Alpine.js-based collapsible sidebar (x-data on
<body>, :class binding on the sidebar <nav>) -- the shared markup has
no generic opinion about a consumer's own client-side framework state.
2026-08-31 14:13:01 +02:00
terminar 671d9bd92d Add PageHeader.NavbarBeforeBrand for a pre-brand navbar element
Needed by yakwui's mobile sidebar-toggle button, which renders before
the brand link rather than after Subtitle.
2026-08-31 10:21:15 +02:00
terminar d3e900389f Add Renderer.ExecuteNamed for rendering a non-base named block
Needed by yasta's SSE system-status stream, which re-renders just its
own fragment block on every tick instead of the full page.
2026-08-29 00:46:28 +02:00
terminar e712ce14fa Correct README sync instructions (root merge, not subtree --prefix) 2026-08-29 00:28:22 +02:00
5 changed files with 100 additions and 20 deletions

No files matched your search

+28 -15
View File
@@ -9,30 +9,43 @@ see `layout.go` for the API (`PageHeader`, `NavItem`, `NewRenderer`,
## Source of truth ## Source of truth
This repo is the **source of truth** for the shared layout. It is mirrored This repo is the **source of truth** for the shared layout. `yak/yak-webui-template`
into the other two meta-repos as `yak-webui-template` and and `yamase/yamase-webui-template` are git-history mirrors of it (each repo's
`yamase-webui-template`, each of which pulls this repo in via `git root mirrors this repo's root — not a subdirectory — created via `git merge
subtree` so layout/CSS changes can be synchronized without duplicating --allow-unrelated-histories`, so the histories are related and can be
design work. Make layout changes *here first*, then push them out — never re-merged incrementally). Each mirror has exactly one intentional local
edit the mirrored copies directly. difference: its `go.mod` `module` line, which names that repo's own import
path instead of this one. Make layout changes *here first*, tag a new
version, then pull the change into each mirror — never edit the mirrored
copies directly.
### Pushing a change out to the mirrors ### Pulling a change into a mirror
From inside `yak` or `yamase` (which already have this repo added as a From inside `yak-webui-template` or `yamase-webui-template` (each already
subtree remote — see below): has a `source` remote pointing back here):
```sh ```sh
git subtree pull --prefix=yak-webui-template webui-template-origin main --squash git fetch source main
# resolve/verify, then: git merge source/main
# resolve the go.mod conflict: keep the mirror's own module line, take
# everything else from source
git checkout --ours go.mod # if go.mod conflicted
git add go.mod
git commit
go test ./...
git push origin main git push origin main
``` ```
### One-time subtree setup (already done for yak/yamase, kept here for ### One-time mirror setup (already done, kept here for reference/re-setup)
reference/re-setup)
```sh ```sh
git remote add webui-template-origin https://teruna.dev/yetano/yetano-webui-template.git git init -b main
git subtree add --prefix=yak-webui-template webui-template-origin main --squash git remote add source git@teruna.dev:yetano/yetano-webui-template.git
git fetch source main
git merge --allow-unrelated-histories -m "Initial mirror of yetano-webui-template" source/main
go mod edit -module teruna.dev/<org>/<org>-webui-template
git remote add origin git@teruna.dev:<org>/<org>-webui-template.git
git push -u origin main
``` ```
## What's shared vs. what stays per-project ## What's shared vs. what stays per-project
+20 -1
View File
@@ -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
@@ -53,10 +54,21 @@ type PageHeader struct {
FlashKind string // "ok" or "error"; meaningless if Flash == "" FlashKind string // "ok" or "error"; meaningless if Flash == ""
Username string Username string
LogoutPath string // BasePath-relative, e.g. "/logout"; "" hides the logout link LogoutPath string // BasePath-relative, e.g. "/logout"; "" hides the logout link
NavbarBeforeBrand template.HTML // extra markup rendered before the brand link, e.g. a mobile sidebar toggle
NavbarLeftExtra template.HTML // extra markup rendered after Subtitle in the navbar's left group NavbarLeftExtra template.HTML // extra markup rendered after Subtitle in the navbar's left group
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:
@@ -92,11 +104,18 @@ func NewRenderer(pagesFS fs.FS, pageDir string, pageNames []string, pageSuffix s
// Page renders the full page (layout + content) for the given page name. // Page renders the full page (layout + content) for the given page name.
func (r *Renderer) Page(w io.Writer, page string, data any) error { func (r *Renderer) Page(w io.Writer, page string, data any) error {
return r.ExecuteNamed(w, page, "base", data)
}
// ExecuteNamed renders the named block (not necessarily "base") from
// page's parsed template set -- e.g. an SSE handler re-rendering just a
// page's own fragment block on every tick instead of the full page.
func (r *Renderer) ExecuteNamed(w io.Writer, page, name string, data any) error {
t, ok := r.pages[page] t, ok := r.pages[page]
if !ok { if !ok {
return fmt.Errorf("layout: unknown page %q", page) return fmt.Errorf("layout: unknown page %q", page)
} }
return t.ExecuteTemplate(w, "base", data) return t.ExecuteTemplate(w, name, data)
} }
// StaticHandler serves the shared vendored static assets // StaticHandler serves the shared vendored static assets
+48 -1
View File
@@ -11,7 +11,8 @@ import (
func testPagesFS() fstest.MapFS { func testPagesFS() fstest.MapFS {
return fstest.MapFS{ return fstest.MapFS{
"pages/dashboard.html.tmpl": &fstest.MapFile{Data: []byte( "pages/dashboard.html.tmpl": &fstest.MapFile{Data: []byte(
`{{define "content"}}<p>hello {{.Title}}</p>{{end}}`, `{{define "content"}}<p>hello {{.Title}}</p>{{end}}` +
`{{define "fragment"}}<p>fragment {{.Title}}</p>{{end}}`,
)}, )},
} }
} }
@@ -46,6 +47,8 @@ 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) { 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)
@@ -53,6 +56,50 @@ func TestNewRendererAndPage(t *testing.T) {
} }
} }
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) {
t.Errorf("rendered output missing %q\n---\n%s", want, out)
}
}
}
func TestExecuteNamed(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
if err := r.ExecuteNamed(&buf, "dashboard", "fragment", PageHeader{Title: "Dashboard"}); err != nil {
t.Fatalf("ExecuteNamed: %v", err)
}
if got, want := buf.String(), "<p>fragment Dashboard</p>"; got != want {
t.Errorf("ExecuteNamed rendered %q, want %q", got, want)
}
}
func TestPageUnknown(t *testing.T) { func TestPageUnknown(t *testing.T) {
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs()) r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
if err != nil { if err != nil {
+3 -3
View File
@@ -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">
@@ -10,11 +10,11 @@
<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
View File
@@ -2,6 +2,7 @@
<nav class="navbar navbar-dark navbar-expand"> <nav class="navbar navbar-dark navbar-expand">
<div class="container-fluid d-flex justify-content-between align-items-center"> <div class="container-fluid d-flex justify-content-between align-items-center">
<div class="d-flex align-items-center"> <div class="d-flex align-items-center">
{{.NavbarBeforeBrand}}
<a class="navbar-brand" href="{{.BasePath}}/">{{.Brand}}</a> <a class="navbar-brand" href="{{.BasePath}}/">{{.Brand}}</a>
{{if .Subtitle}}<span class="navbar-subtitle">{{.Subtitle}}</span>{{end}} {{if .Subtitle}}<span class="navbar-subtitle">{{.Subtitle}}</span>{{end}}
{{.NavbarLeftExtra}} {{.NavbarLeftExtra}}