Files
hspguard/internal/web/templates.go

28 lines
647 B
Go

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)
}
}