diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index 9d8207f..fb30302 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -6,6 +6,18 @@ import ( "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 @@ -28,10 +40,14 @@ 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") + render.RenderTemplate(w, "home.page.tmpl", &TemplateData{}) } // About is the about page handler func (m *Repository) About(w http.ResponseWriter, r *http.Request) { - render.RenderTemplate(w, "about.page.tmpl") + // perform some logic + stringMap := make(map[string]string) + stringMap["test"] = "Hello world!" + // send the data to the template + render.RenderTemplate(w, "about.page.tmpl", &TemplateData{StringMap: stringMap}) } diff --git a/pkg/render/render.go b/pkg/render/render.go index 89ea25b..8e8a78d 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -3,6 +3,7 @@ package render import ( "bytes" "go-udemy-web-1/pkg/config" + "go-udemy-web-1/pkg/handlers" "html/template" "log" "net/http" @@ -17,7 +18,7 @@ func NewTemplates(a *config.AppConfig) { } // RenderTemplate renders a HTML template file -func RenderTemplate(w http.ResponseWriter, tmpl string) { +func RenderTemplate(w http.ResponseWriter, tmpl string, td *handlers.TemplateData) { var tc map[string]*template.Template if app.UseCache { // get the template cache from the app config diff --git a/templates/about.page.tmpl b/templates/about.page.tmpl index 4c0281b..e90ce29 100644 --- a/templates/about.page.tmpl +++ b/templates/about.page.tmpl @@ -5,6 +5,8 @@

This is the about page

This is the content

+ +

This came from the template: {{index .StringMap "test"}}