Sharing data with templates: cycle import error

This commit is contained in:
Muyao CHEN
2024-06-28 10:40:47 +02:00
parent 1dd22ba8db
commit 668e88e578
3 changed files with 22 additions and 3 deletions

View File

@ -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})
}