Sharing data with templates: correct import cycle
This commit is contained in:
@ -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})
|
||||
}
|
||||
|
Reference in New Issue
Block a user