From b9c8c2592dbc54d7986a25621c14ff3e2be90600 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Fri, 28 Jun 2024 10:48:52 +0200 Subject: [PATCH] Sharing data with templates: correct import cycle --- pkg/handlers/handlers.go | 17 +++-------------- pkg/models/templatedata.go | 13 +++++++++++++ pkg/render/render.go | 14 +++++++++++--- 3 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 pkg/models/templatedata.go diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index fb30302..7622790 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -2,22 +2,11 @@ package handlers import ( "go-udemy-web-1/pkg/config" + "go-udemy-web-1/pkg/models" "go-udemy-web-1/pkg/render" "net/http" ) -// TemplateData holds data sent from handlers to templates -type TemplateData struct { - StringMap map[string]string - IntMap map[string]int - FloatMap map[string]float32 - Data map[string]interface{} - CSRFToken string - Flash string - Warning string - Error string -} - // Repo the repository used by the handlers var Repo *Repository @@ -40,7 +29,7 @@ func NewHandlers(r *Repository) { // Home is the about page handler func (m *Repository) Home(w http.ResponseWriter, r *http.Request) { - render.RenderTemplate(w, "home.page.tmpl", &TemplateData{}) + render.RenderTemplate(w, "home.page.tmpl", &models.TemplateData{}) } // About is the about page handler @@ -49,5 +38,5 @@ func (m *Repository) About(w http.ResponseWriter, r *http.Request) { stringMap := make(map[string]string) stringMap["test"] = "Hello world!" // send the data to the template - render.RenderTemplate(w, "about.page.tmpl", &TemplateData{StringMap: stringMap}) + render.RenderTemplate(w, "about.page.tmpl", &models.TemplateData{StringMap: stringMap}) } diff --git a/pkg/models/templatedata.go b/pkg/models/templatedata.go new file mode 100644 index 0000000..f800586 --- /dev/null +++ b/pkg/models/templatedata.go @@ -0,0 +1,13 @@ +package models + +// TemplateData holds data sent from handlers to templates +type TemplateData struct { + StringMap map[string]string + IntMap map[string]int + FloatMap map[string]float32 + Data map[string]interface{} + CSRFToken string + Flash string + Warning string + Error string +} diff --git a/pkg/render/render.go b/pkg/render/render.go index 8e8a78d..84f8bc7 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -3,7 +3,7 @@ package render import ( "bytes" "go-udemy-web-1/pkg/config" - "go-udemy-web-1/pkg/handlers" + "go-udemy-web-1/pkg/models" "html/template" "log" "net/http" @@ -17,8 +17,13 @@ func NewTemplates(a *config.AppConfig) { app = a } +// AddDefaultData adds default template data +func AddDefaultData(td *models.TemplateData) *models.TemplateData { + return td +} + // RenderTemplate renders a HTML template file -func RenderTemplate(w http.ResponseWriter, tmpl string, td *handlers.TemplateData) { +func RenderTemplate(w http.ResponseWriter, tmpl string, td *models.TemplateData) { var tc map[string]*template.Template if app.UseCache { // get the template cache from the app config @@ -36,7 +41,10 @@ func RenderTemplate(w http.ResponseWriter, tmpl string, td *handlers.TemplateDat // Write to a buffer to make sure that the template can be read and // written successfully buf := new(bytes.Buffer) - err := t.Execute(buf, nil) + + td = AddDefaultData(td) + + err := t.Execute(buf, td) if err != nil { log.Println(err) }