package router import ( "html/template" "net/http" "git.0cd.xyz/michael/gtools/tpl" "github.com/gorilla/csrf" ) // Page web page type Page struct { App interface{} CSRFField template.HTML Content interface{} } // HandlePage handles page requests with body func (re *Router) HandlePage(title string, body interface{}) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { re.Response(&Response{ Title: title, Body: body, }, http.StatusOK).ServeHTTP(w, r) } } // RenderTemplate renders html templates func (re *Router) RenderTemplate(data interface{}) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { tmpl, err := tpl.Load(re.tplopts) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := tmpl.Execute(w, &Page{ App: re.page, CSRFField: csrf.TemplateField(r), Content: data, }); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } }