From 8bc58eedbe97acc33c078c926cbd1ef93d3f8721 Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Mon, 12 Aug 2024 18:55:57 -0400 Subject: [PATCH] Add request data to template Execute function --- controllers/static.go | 6 +++--- controllers/users.go | 4 ++-- views/template.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/controllers/static.go b/controllers/static.go index 9e28c08..5aa14d6 100644 --- a/controllers/static.go +++ b/controllers/static.go @@ -8,7 +8,7 @@ import ( ) type Template interface { - Execute(w http.ResponseWriter, data interface{}) + Execute(w http.ResponseWriter, r *http.Request, data interface{}) } func StaticController(templatePath ...string) http.HandlerFunc { @@ -20,7 +20,7 @@ func StaticController(templatePath ...string) http.HandlerFunc { panic(err) } - return func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) } + return func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, r, nil) } } func FAQ(templatePath ...string) http.HandlerFunc { @@ -47,6 +47,6 @@ func FAQ(templatePath ...string) http.HandlerFunc { } return func(w http.ResponseWriter, r *http.Request) { - tpl.Execute(w, questions) + tpl.Execute(w, r, questions) } } diff --git a/controllers/users.go b/controllers/users.go index 1683a5a..ccf2eeb 100644 --- a/controllers/users.go +++ b/controllers/users.go @@ -27,7 +27,7 @@ func (u Users) GetSignup(w http.ResponseWriter, r *http.Request) { } data.Email = r.FormValue("email") data.CSRFField = csrf.TemplateField(r) - u.Templates.Signup.Execute(w, data) + u.Templates.Signup.Execute(w, r, data) } func (u Users) PostSignup(w http.ResponseWriter, r *http.Request) { @@ -49,7 +49,7 @@ func (u Users) GetSignin(w http.ResponseWriter, r *http.Request) { } data.Email = r.FormValue("email") data.CSRFField = csrf.TemplateField(r) - u.Templates.Signin.Execute(w, data) + u.Templates.Signin.Execute(w, r, data) } func (u Users) PostSignin(w http.ResponseWriter, r *http.Request) { var data struct { diff --git a/views/template.go b/views/template.go index 6ab7254..3b90ad0 100644 --- a/views/template.go +++ b/views/template.go @@ -14,7 +14,7 @@ type Template struct { htmlTpl *template.Template } -func (t Template) Execute(w http.ResponseWriter, data interface{}) { +func (t Template) Execute(w http.ResponseWriter, r *http.Request, data interface{}) { w.Header().Set("Content-Type", "text/html; charset=utf8") err := t.htmlTpl.Execute(w, data) if err != nil {