Add templating
This commit is contained in:
86
internal/server/template_data.go
Normal file
86
internal/server/template_data.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package server
|
||||
|
||||
import "html/template"
|
||||
|
||||
// This file defines the data structures and helper functions for the template system.
|
||||
//
|
||||
// The template system uses Go's html/template package to provide:
|
||||
// - Dynamic menu bars that can be customized per page
|
||||
// - Template inheritance via layout.html
|
||||
// - Type-safe data passing via structs
|
||||
//
|
||||
// To create a new page with a custom menu:
|
||||
// 1. Create a PageData struct with your menu configuration
|
||||
// 2. Call s.renderTemplate(w, status, "yourpage.html", data)
|
||||
// 3. In yourpage.html, use {{template "layout" .}} and define content blocks
|
||||
//
|
||||
// See TEMPLATE_GUIDE.md for detailed examples and usage patterns.
|
||||
|
||||
// PageData holds the data passed to page templates.
|
||||
type PageData struct {
|
||||
Title string
|
||||
MenuGroups []MenuGroup
|
||||
ShowClock bool
|
||||
}
|
||||
|
||||
// MenuGroup represents a dropdown menu in the navbar.
|
||||
type MenuGroup struct {
|
||||
LabelHTML template.HTML
|
||||
Items []MenuItem
|
||||
}
|
||||
|
||||
// MenuItem represents a single menu item.
|
||||
type MenuItem struct {
|
||||
LabelHTML template.HTML
|
||||
URL string
|
||||
IsDivider bool
|
||||
}
|
||||
|
||||
// DefaultMenuBar returns the standard menu configuration.
|
||||
func DefaultMenuBar() PageData {
|
||||
return PageData{
|
||||
ShowClock: true,
|
||||
MenuGroups: []MenuGroup{
|
||||
{
|
||||
LabelHTML: template.HTML(`<span class="red-168-text">F</span>ile`),
|
||||
Items: []MenuItem{
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">N</span>ew`), URL: "#!"},
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">O</span>pen`), URL: "#!"},
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">S</span>ave`), URL: "#!"},
|
||||
{LabelHTML: template.HTML(`Save <span class="red-168-text">A</span>s`), URL: "#!"},
|
||||
{IsDivider: true},
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">E</span>xit`), URL: "#!"},
|
||||
},
|
||||
},
|
||||
{
|
||||
LabelHTML: template.HTML(`<span class="red-168-text">E</span>dit`),
|
||||
Items: []MenuItem{
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">C</span>ut`), URL: "#!"},
|
||||
{LabelHTML: template.HTML(`C<span class="red-168-text">o</span>py`), URL: "#!"},
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">P</span>aste`), URL: "#!"},
|
||||
},
|
||||
},
|
||||
{
|
||||
LabelHTML: template.HTML(`<span class="red-168-text">H</span>elp`),
|
||||
Items: []MenuItem{
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">D</span>ocumentation`), URL: "#!"},
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">A</span>bout`), URL: "#!"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// AdminMenuBar returns a menu configuration with admin options.
|
||||
func AdminMenuBar() PageData {
|
||||
data := DefaultMenuBar()
|
||||
data.MenuGroups = append(data.MenuGroups, MenuGroup{
|
||||
LabelHTML: template.HTML(`<span class="red-168-text">A</span>dmin`),
|
||||
Items: []MenuItem{
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">U</span>sers`), URL: "/admin/users"},
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">S</span>ettings`), URL: "/admin/settings"},
|
||||
{LabelHTML: template.HTML(`<span class="red-168-text">L</span>ogs`), URL: "/admin/logs"},
|
||||
},
|
||||
})
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user