feat: render template web function

This commit is contained in:
2025-05-18 14:05:42 +02:00
parent 299e586d69
commit b7dbe1ef0d

27
internal/web/templates.go Normal file
View File

@ -0,0 +1,27 @@
package web
import (
"fmt"
"html/template"
"net/http"
"path/filepath"
)
func RenderTemplate(w http.ResponseWriter, page string, data map[string]any) {
base, err := template.ParseGlob(filepath.Join("templates", "layout", "*.html"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := base.ParseFiles(filepath.Join("templates", "pages", fmt.Sprintf("%s.html", page)))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.ExecuteTemplate(w, "base", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}