render

package
v0.18.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package render provides HTML template rendering with layout support, flash message handling, and helper functions for the admin interface.

Index

Constants

View Source
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

func FormatBytes(size int64) string

FormatBytes formats bytes into a human-readable string.

func FormatDateTime added in v0.9.0

func FormatDateTime(t time.Time) string

FormatDateTime formats a time value as "Jan 2, 2006 3:04 PM".

func Templ added in v0.9.0

func Templ(w http.ResponseWriter, r *http.Request, component templ.Component)

Templ renders a templ component as an HTTP response. This is used by modules to render templ-based admin pages.

Types

type AdminLangOption added in v0.9.0

type AdminLangOption struct {
	Code string
	Name string
}

AdminLangOption represents a language option for the admin UI language switcher.

type Breadcrumb struct {
	Label  string
	URL    string
	Active bool
}

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 New

func New(cfg Config) (*Renderer, error)

New creates a new Renderer with parsed templates.

func (*Renderer) AddTemplateFuncs

func (r *Renderer) AddTemplateFuncs(funcs template.FuncMap)

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

func (r *Renderer) FormatDateTimeLocale(t any, lang string) string

FormatDateTimeLocale formats a time value as a localized datetime string.

func (*Renderer) GetAdminLang

func (r *Renderer) GetAdminLang(req *http.Request) string

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

func (r *Renderer) HcaptchaEnabled() bool

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

func (r *Renderer) HcaptchaWidgetHTML() string

HcaptchaWidgetHTML returns the hCaptcha widget HTML string. It calls the hcaptchaWidget template function if registered, otherwise returns empty string.

func (*Renderer) InvalidateMenuCache

func (r *Renderer) InvalidateMenuCache(slug string)

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

func (r *Renderer) PopSessionData(req *http.Request, key string) map[string]string

PopSessionData retrieves and removes data from the session. Returns nil if no data found.

func (*Renderer) ReloadTemplates

func (r *Renderer) ReloadTemplates() error

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

func (r *Renderer) SentinelIsActive() bool

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

func (r *Renderer) SentinelIsIPBanned(ip string) bool

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

func (r *Renderer) SentinelIsIPWhitelisted(ip string) bool

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

func (r *Renderer) SetAdminLang(req *http.Request, lang string)

SetAdminLang sets the admin UI language preference in the session.

func (*Renderer) SetFlash

func (r *Renderer) SetFlash(req *http.Request, message, flashType string)

SetFlash sets a flash message in the session.

func (*Renderer) SetSessionData

func (r *Renderer) SetSessionData(req *http.Request, key string, data map[string]string)

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

func (r *Renderer) TemplateFuncs() template.FuncMap

TemplateFuncs returns custom template functions for external use.

type SidebarModule

type SidebarModule struct {
	Name     string
	Label    string
	AdminURL string
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL