add templating engine
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package goblocks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (a *App) JSON(w http.ResponseWriter, code int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func (a *App) HTML(w http.ResponseWriter, code int, name string, td *TemplateData) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
err := a.Templates.Template(w, name, td)
|
||||
if err != nil {
|
||||
slog.Error("error rendering template", "error", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(code)
|
||||
}
|
||||
Reference in New Issue
Block a user