Extract page layout

This commit is contained in:
Pijus Kamandulis
2025-06-23 18:42:00 +03:00
parent be637f4540
commit f66fbcc454
8 changed files with 190 additions and 282 deletions
+5 -8
View File
@@ -5,7 +5,6 @@ import (
"net/http"
"pool-stats/database"
"pool-stats/helpers"
"pool-stats/models"
)
@@ -14,13 +13,10 @@ type IndexPageData struct {
}
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("index").Funcs(template.FuncMap{
"humanDiff": helpers.HumanDiff,
"formatCreateDate": helpers.FormatCreateDate,
})
tmpl, err := tmpl.ParseFiles("templates/index.html")
tmpl, err := template.Must(ws.templates.Clone()).ParseFiles("templates/index.html")
if err != nil {
http.Error(w, "Failed to load template", 500)
http.Error(w, "Failed to parse template", 500)
println("Error parsing template:", err.Error())
return
}
@@ -33,7 +29,8 @@ func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
indexData := IndexPageData{
Stats: tws,
}
if err := tmpl.Execute(w, indexData); err != nil {
if err := tmpl.ExecuteTemplate(w, "index.html", indexData); err != nil {
http.Error(w, "Failed to render template", 500)
println("Error rendering template:", err.Error())
return