Documentation
¶
Overview ¶
Package render provides HTML template rendering with layout support, flash message handling, and helper functions for the admin interface.
Index ¶
- Constants
- func FormatBytes(size int64) string
- func FormatDateTime(t time.Time) string
- func Templ(w http.ResponseWriter, r *http.Request, component templ.Component)
- type AdminLangOption
- type Breadcrumb
- type Config
- type Renderer
- func (r *Renderer) AddTemplateFuncs(funcs template.FuncMap)
- func (r *Renderer) AdminLangOptions() []AdminLangOption
- func (r *Renderer) BuildPageContext(req *http.Request, title string, breadcrumbs []Breadcrumb) *adminviews.PageContext
- func (r *Renderer) FormatDateTimeLocale(t any, lang string) string
- func (r *Renderer) GetAdminLang(req *http.Request) string
- func (r *Renderer) GetMenuService() *service.MenuService
- func (r *Renderer) HcaptchaEnabled() bool
- func (r *Renderer) HcaptchaWidgetHTML() string
- func (r *Renderer) InvalidateMenuCache(slug string)
- func (r *Renderer) ListSidebarModules() []SidebarModule
- func (r *Renderer) PopSessionData(req *http.Request, key string) map[string]string
- func (r *Renderer) ReloadTemplates() error
- func (r *Renderer) Render(w http.ResponseWriter, req *http.Request, name string, data TemplateData) error
- func (r *Renderer) RenderError(w http.ResponseWriter, req *http.Request, statusCode int, title string)
- func (r *Renderer) RenderForbidden(w http.ResponseWriter, req *http.Request)
- func (r *Renderer) RenderInternalError(w http.ResponseWriter, req *http.Request)
- func (r *Renderer) RenderNotFound(w http.ResponseWriter, req *http.Request)
- func (r *Renderer) RenderPage(w http.ResponseWriter, req *http.Request, name string, data TemplateData)
- func (r *Renderer) SentinelIsActive() bool
- func (r *Renderer) SentinelIsIPBanned(ip string) bool
- func (r *Renderer) SentinelIsIPWhitelisted(ip string) bool
- func (r *Renderer) SetAdminLang(req *http.Request, lang string)
- func (r *Renderer) SetFlash(req *http.Request, message, flashType string)
- func (r *Renderer) SetSessionData(req *http.Request, key string, data map[string]string)
- func (r *Renderer) SetSidebarModuleProvider(provider SidebarModuleProvider)
- func (r *Renderer) TemplateFuncs() template.FuncMap
- type SidebarModule
- type SidebarModuleProvider
- type TemplateData
Constants ¶
const SessionKeyAdminLang = "admin_lang"
SessionKeyAdminLang is the session key for storing admin UI language preference.
Variables ¶
This section is empty.
Functions ¶
func FormatBytes ¶ added in v0.9.0
FormatBytes formats bytes into a human-readable string.
func FormatDateTime ¶ added in v0.9.0
FormatDateTime formats a time value as "Jan 2, 2006 3:04 PM".
Types ¶
type AdminLangOption ¶ added in v0.9.0
AdminLangOption represents a language option for the admin UI language switcher.
type Breadcrumb ¶
Breadcrumb represents a single breadcrumb item.
type Config ¶
type Config struct {
TemplatesFS fs.FS
SessionManager *scs.SessionManager
DB *sql.DB
IsDev bool
MenuService *service.MenuService // Optional: shared menu service for cache consistency
}
Config holds renderer configuration.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer handles template rendering with caching.
func (*Renderer) AddTemplateFuncs ¶
AddTemplateFuncs adds additional template functions (e.g., from modules).
func (*Renderer) AdminLangOptions ¶ added in v0.9.0
func (r *Renderer) AdminLangOptions() []AdminLangOption
AdminLangOptions returns the list of admin UI languages.
func (*Renderer) BuildPageContext ¶ added in v0.9.0
func (r *Renderer) BuildPageContext(req *http.Request, title string, breadcrumbs []Breadcrumb) *adminviews.PageContext
BuildPageContext creates a PageContext for templ views from the request state. This is used by modules to render templ-based admin pages.
func (*Renderer) FormatDateTimeLocale ¶ added in v0.9.0
FormatDateTimeLocale formats a time value as a localized datetime string.
func (*Renderer) GetAdminLang ¶
GetAdminLang gets the admin UI language preference from the session.
func (*Renderer) GetMenuService ¶
func (r *Renderer) GetMenuService() *service.MenuService
GetMenuService returns the menu service for sharing with other handlers.
func (*Renderer) HcaptchaEnabled ¶ added in v0.9.0
HcaptchaEnabled returns whether hCaptcha is enabled. It calls the hcaptchaEnabled template function if registered, otherwise returns false.
func (*Renderer) HcaptchaWidgetHTML ¶ added in v0.9.0
HcaptchaWidgetHTML returns the hCaptcha widget HTML string. It calls the hcaptchaWidget template function if registered, otherwise returns empty string.
func (*Renderer) InvalidateMenuCache ¶
InvalidateMenuCache clears the cached menu by slug, or all menus if slug is empty.
func (*Renderer) ListSidebarModules ¶ added in v0.9.0
func (r *Renderer) ListSidebarModules() []SidebarModule
ListSidebarModules returns the sidebar modules from the provider.
func (*Renderer) PopSessionData ¶
PopSessionData retrieves and removes data from the session. Returns nil if no data found.
func (*Renderer) ReloadTemplates ¶
ReloadTemplates re-parses all templates with the current template functions. This should be called after adding module template functions.
func (*Renderer) Render ¶
func (r *Renderer) Render(w http.ResponseWriter, req *http.Request, name string, data TemplateData) error
Render renders a template with the given data.
func (*Renderer) RenderError ¶
func (r *Renderer) RenderError(w http.ResponseWriter, req *http.Request, statusCode int, title string)
RenderError renders an error page with the specified status code.
func (*Renderer) RenderForbidden ¶
func (r *Renderer) RenderForbidden(w http.ResponseWriter, req *http.Request)
RenderForbidden renders a 403 Forbidden page.
func (*Renderer) RenderInternalError ¶
func (r *Renderer) RenderInternalError(w http.ResponseWriter, req *http.Request)
RenderInternalError renders a 500 Internal Server Error page.
func (*Renderer) RenderNotFound ¶
func (r *Renderer) RenderNotFound(w http.ResponseWriter, req *http.Request)
RenderNotFound renders a 404 Not Found page.
func (*Renderer) RenderPage ¶
func (r *Renderer) RenderPage(w http.ResponseWriter, req *http.Request, name string, data TemplateData)
RenderPage renders a template and handles errors by logging and returning a 500 response. This is a convenience wrapper around Render that eliminates boilerplate error handling.
func (*Renderer) SentinelIsActive ¶ added in v0.9.0
SentinelIsActive returns whether the sentinel module is active. It calls the sentinelIsActive template function if registered, otherwise returns false.
func (*Renderer) SentinelIsIPBanned ¶ added in v0.9.0
SentinelIsIPBanned returns whether an IP is banned by the sentinel module. It calls the sentinelIsIPBanned template function if registered, otherwise returns false.
func (*Renderer) SentinelIsIPWhitelisted ¶ added in v0.9.0
SentinelIsIPWhitelisted returns whether an IP is whitelisted by the sentinel module. It calls the sentinelIsIPWhitelisted template function if registered, otherwise returns false.
func (*Renderer) SetAdminLang ¶
SetAdminLang sets the admin UI language preference in the session.
func (*Renderer) SetSessionData ¶
SetSessionData stores arbitrary data in the session under the given key. Data is stored as JSON-encoded string.
func (*Renderer) SetSidebarModuleProvider ¶
func (r *Renderer) SetSidebarModuleProvider(provider SidebarModuleProvider)
SetSidebarModuleProvider sets the provider for sidebar modules. This is called after modules are initialized since they're registered after the renderer is created.
func (*Renderer) TemplateFuncs ¶
TemplateFuncs returns custom template functions for external use.
type SidebarModule ¶
SidebarModule represents a module to display in the admin sidebar.
type SidebarModuleProvider ¶
type SidebarModuleProvider interface {
ListSidebarModules() []SidebarModule
}
SidebarModuleProvider provides sidebar modules for the renderer.
type TemplateData ¶
type TemplateData struct {
Title string
Data any
User any // Current authenticated user (available in all admin templates)
Flash string
FlashType string
CurrentYear int
CSRFToken string // CSRF token value
CSRFField template.HTML // Hidden input field with CSRF token
SiteName string // Site name from config
Breadcrumbs []Breadcrumb // Breadcrumb navigation
CurrentPath string // Current request path for active link detection
AdminLang string // Admin UI language code (en, ru, etc.)
SidebarModules []SidebarModule // Modules to display in admin sidebar
CSPNonce string // CSP nonce for inline scripts
}
TemplateData holds data passed to templates.