49 lines
928 B
Go
49 lines
928 B
Go
package helpers
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
"synlotto-website/models"
|
|
)
|
|
|
|
func TemplateFuncs() template.FuncMap {
|
|
return template.FuncMap{
|
|
"plus1": func(i int) int { return i + 1 },
|
|
"minus1": func(i int) int {
|
|
if i > 1 {
|
|
return i - 1
|
|
}
|
|
return 0
|
|
},
|
|
"mul": func(a, b int) int { return a * b },
|
|
"add": func(a, b int) int { return a + b },
|
|
"min": func(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
},
|
|
}
|
|
}
|
|
|
|
func TemplateContext(w http.ResponseWriter, r *http.Request) 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)
|
|
}
|
|
|
|
var currentUser *models.User
|
|
if userId, ok := session.Values["user_id"].(int); ok {
|
|
currentUser = models.GetUserByID(userId)
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"Flash": flash,
|
|
"User": currentUser,
|
|
}
|
|
}
|