diff --git a/internal/web/templates.go b/internal/web/templates.go new file mode 100644 index 0000000..1d711d5 --- /dev/null +++ b/internal/web/templates.go @@ -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) + } +} +