Files
nfeeder/internal/web/utils.go
T
jasonhilder a8862721cd Initial project structure commit.
Static directory for public folders and business logic for the app
within internal, split into domain specific folders to keep clear
seperation of concerns.
2026-04-23 08:09:09 +02:00

33 lines
902 B
Go

package web
import (
"html/template"
"net/http"
"path/filepath"
)
// Renders a full page by combining the base template with a page template
// Parsed together so the page can define blocks needed for base template
func render(w http.ResponseWriter, _ *http.Request, page string, data any) {
files := []string{
filepath.Join("internal", "templates", "layouts", "base.html"),
filepath.Join("internal", "templates", "pages", page+".html"),
}
tmpl, err := template.ParseFiles(files...)
if err != nil {
http.Error(w, "template error: "+err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
if err := tmpl.ExecuteTemplate(w, "base", data); err != nil {
http.Error(w, "render error: "+err.Error(), http.StatusInternalServerError)
}
}
func view(w http.ResponseWriter, r *http.Request, page string, data any) {
render(w, r, page, data)
}