// Package layout provides the shared server-rendered web UI chrome (base // layout, navbar, sidebar, footer) used by every Yetano/Yak/Yamase // ops-tool admin dashboard: yasta, yasl, yaiam, yakwui, and yamase-web. // // Consumers embed PageHeader in their own per-page data struct and use // NewRenderer to parse the shared layout templates together with their // own page-specific templates. Page-content styling (cards, tables, // badges, forms, ...) stays each project's own concern; only the chrome // that base/navbar/sidebar/footer actually render is shared here. package layout import ( "embed" "fmt" "html/template" "io" "io/fs" "net/http" ) //go:embed templates/layout var templateFS embed.FS //go:embed static var staticFS embed.FS // LayoutGlob is the glob NewRenderer uses to pull in the shared // base/navbar/sidebar/footer templates. const LayoutGlob = "templates/layout/*.tmpl" // NavItem is one sidebar entry. type NavItem struct { ID string // matches PageHeader.ActiveNav to mark this item active Label string Href string // BasePath-relative, e.g. "/tasks" } // PageHeader holds the fields every page's own data struct should embed // so the shared base/navbar/sidebar/footer templates can render // regardless of which project or page is being rendered. Zero-valued // fields simply render nothing (e.g. Username == "" hides the // username/logout group). type PageHeader struct { Title string Name string // lowercase product name, e.g. "yasta"; used in and the footer Brand string // navbar brand text, e.g. "YASTA" BrandInitial string // single-letter favicon glyph, e.g. "y" Subtitle string BasePath string NavItems []NavItem ActiveNav string Flash string FlashKind string // "ok" or "error"; meaningless if Flash == "" Username string LogoutPath string // BasePath-relative, e.g. "/logout"; "" hides the logout link 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 ExtraHeadHTML template.HTML // extra <link>/<script> tags appended to <head> ExtraBodyHTML template.HTML // extra <script> tags appended before </body> } // Renderer holds one independently parsed *template.Template per page: // the shared layout files plus that one page's own file. Parsing each // page separately -- rather than one shared template.Template built from // every file -- keeps each page's "content" block (every page defines // one) scoped to that page instead of colliding in a single global // template namespace. type Renderer struct { pages map[string]*template.Template } // NewRenderer parses, for each name in pageNames, the shared layout // templates plus pagesFS's <pageDir>/<name><pageSuffix> into its own // Renderer entry. funcs must supply everything the templates call, // including "buildInfo" (used by the shared footer). func NewRenderer(pagesFS fs.FS, pageDir string, pageNames []string, pageSuffix string, funcs template.FuncMap) (*Renderer, error) { r := &Renderer{pages: make(map[string]*template.Template, len(pageNames))} for _, name := range pageNames { t := template.New("base").Funcs(funcs) t, err := t.ParseFS(templateFS, LayoutGlob) if err != nil { return nil, fmt.Errorf("layout: parsing shared layout templates: %w", err) } t, err = t.ParseFS(pagesFS, pageDir+"/"+name+pageSuffix) if err != nil { return nil, fmt.Errorf("layout: parsing page %q: %w", name, err) } r.pages[name] = t } return r, nil } // Page renders the full page (layout + content) for the given page name. func (r *Renderer) Page(w io.Writer, page string, data any) error { t, ok := r.pages[page] if !ok { return fmt.Errorf("layout: unknown page %q", page) } return t.ExecuteTemplate(w, "base", data) } // StaticHandler serves the shared vendored static assets // (bootstrap.min.css, bootstrap.bundle.min.js, and the shared chrome // stylesheet) under prefix. func StaticHandler(prefix string) http.Handler { sub, err := fs.Sub(staticFS, "static") if err != nil { panic(err) // static is embedded at build time; this cannot fail at runtime } return http.StripPrefix(prefix, http.FileServerFS(sub)) }