Refactor: Centralize template loading and improve error handling
- Introduced helpers.LoadTemplateFiles() for consistent layout + topbar rendering - Replaced repeated template.ParseFiles() calls across handlers - Created generic RenderError(w, r, statusCode) helper - Replaced old Render403 with flexible RenderError - Updated AdminOnly middleware to render 403 errors with context - Added 500.html template for graceful panic fallback - Prepared structure for future error codes (404, 429, etc.)
This commit is contained in:
@@ -32,7 +32,6 @@ func TemplateContext(w http.ResponseWriter, r *http.Request, data models.Templat
|
||||
}
|
||||
}
|
||||
|
||||
// TemplateFuncs provides helper functions to be used in templates.
|
||||
func TemplateFuncs() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"plus1": func(i int) int { return i + 1 },
|
||||
@@ -63,7 +62,16 @@ func TemplateFuncs() template.FuncMap {
|
||||
}
|
||||
}
|
||||
|
||||
// SetFlash sets a flash message in session.
|
||||
func LoadTemplateFiles(name string, files ...string) *template.Template {
|
||||
shared := []string{
|
||||
"templates/layout.html",
|
||||
"templates/topbar.html",
|
||||
}
|
||||
all := append(shared, files...)
|
||||
|
||||
return template.Must(template.New(name).Funcs(TemplateFuncs()).ParseFiles(all...))
|
||||
}
|
||||
|
||||
func SetFlash(w http.ResponseWriter, r *http.Request, message string) {
|
||||
session, _ := GetSession(w, r)
|
||||
session.Values["flash"] = message
|
||||
|
||||
Reference in New Issue
Block a user