Refactor: Centralize template context using unified TemplateData struct
- Introduced models.TemplateData for shared user/context state - Moved context construction logic into handlers/template_context.go - Simplified helpers.TemplateContext to accept structured data - Restored and organized template helper functions - Updated affected handlers (main.go, draw_handler.go, notifications.go) - Improved scalability and separation of concerns in template rendering
This commit is contained in:
@@ -4,12 +4,35 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"synlotto-website/models"
|
||||
"synlotto-website/storage"
|
||||
|
||||
"github.com/gorilla/csrf"
|
||||
)
|
||||
|
||||
func TemplateContext(w http.ResponseWriter, r *http.Request, data models.TemplateData) map[string]interface{} {
|
||||
session, _ := GetSession(w, r)
|
||||
|
||||
var flash string
|
||||
if f, ok := session.Values["flash"].(string); ok {
|
||||
flash = f
|
||||
delete(session.Values, "flash")
|
||||
session.Save(r, w)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"CSRFField": csrf.TemplateField(r),
|
||||
"Flash": flash,
|
||||
"User": data.User,
|
||||
"IsAdmin": data.IsAdmin,
|
||||
"NotificationCount": data.NotificationCount,
|
||||
"Notifications": data.Notifications,
|
||||
"MessageCount": data.MessageCount,
|
||||
"Messages": data.Messages,
|
||||
}
|
||||
}
|
||||
|
||||
// TemplateFuncs provides helper functions to be used in templates.
|
||||
func TemplateFuncs() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"plus1": func(i int) int { return i + 1 },
|
||||
@@ -24,8 +47,9 @@ func TemplateFuncs() template.FuncMap {
|
||||
"min": func(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
return b
|
||||
},
|
||||
"intVal": func(p *int) int {
|
||||
if p == nil {
|
||||
@@ -39,42 +63,11 @@ func TemplateFuncs() template.FuncMap {
|
||||
}
|
||||
}
|
||||
|
||||
func TemplateContext(w http.ResponseWriter, r *http.Request) map[string]interface{} {
|
||||
// SetFlash sets a flash message in session.
|
||||
func SetFlash(w http.ResponseWriter, r *http.Request, message string) {
|
||||
session, _ := GetSession(w, r)
|
||||
|
||||
var flash string
|
||||
if f, ok := session.Values["flash"].(string); ok {
|
||||
flash = f
|
||||
delete(session.Values, "flash")
|
||||
session.Save(r, w)
|
||||
}
|
||||
|
||||
var currentUser *models.User
|
||||
var isAdmin bool
|
||||
var notificationCount int
|
||||
var notifications []models.Notification
|
||||
|
||||
switch v := session.Values["user_id"].(type) {
|
||||
case int:
|
||||
currentUser = models.GetUserByID(v)
|
||||
case int64:
|
||||
currentUser = models.GetUserByID(int(v))
|
||||
}
|
||||
|
||||
if currentUser != nil {
|
||||
isAdmin = currentUser.IsAdmin
|
||||
notificationCount = storage.GetNotificationCount(currentUser.Id)
|
||||
notifications = storage.GetRecentNotifications(currentUser.Id, 15)
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"CSRFField": csrf.TemplateField(r),
|
||||
"Flash": flash,
|
||||
"User": currentUser,
|
||||
"IsAdmin": isAdmin,
|
||||
"NotificationCount": notificationCount,
|
||||
"Notifications": notifications,
|
||||
}
|
||||
session.Values["flash"] = message
|
||||
session.Save(r, w)
|
||||
}
|
||||
|
||||
func InSlice(n int, list []int) bool {
|
||||
@@ -83,7 +76,6 @@ func InSlice(n int, list []int) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -107,9 +99,3 @@ func rangeClass(n int) string {
|
||||
return "50-plus"
|
||||
}
|
||||
}
|
||||
|
||||
func SetFlash(w http.ResponseWriter, r *http.Request, message string) {
|
||||
session, _ := GetSession(w, r)
|
||||
session.Values["flash"] = message
|
||||
session.Save(r, w)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user