Sharing data with templates: correct import cycle

This commit is contained in:
Muyao CHEN 2024-06-28 10:48:52 +02:00
parent 668e88e578
commit b9c8c2592d
3 changed files with 27 additions and 17 deletions

View File

@ -2,22 +2,11 @@ package handlers
import ( import (
"go-udemy-web-1/pkg/config" "go-udemy-web-1/pkg/config"
"go-udemy-web-1/pkg/models"
"go-udemy-web-1/pkg/render" "go-udemy-web-1/pkg/render"
"net/http" "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 // Repo the repository used by the handlers
var Repo *Repository var Repo *Repository
@ -40,7 +29,7 @@ func NewHandlers(r *Repository) {
// Home is the about page handler // Home is the about page handler
func (m *Repository) Home(w http.ResponseWriter, r *http.Request) { 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 // 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 := make(map[string]string)
stringMap["test"] = "Hello world!" stringMap["test"] = "Hello world!"
// send the data to the template // 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})
} }

View File

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

View File

@ -3,7 +3,7 @@ package render
import ( import (
"bytes" "bytes"
"go-udemy-web-1/pkg/config" "go-udemy-web-1/pkg/config"
"go-udemy-web-1/pkg/handlers" "go-udemy-web-1/pkg/models"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
@ -17,8 +17,13 @@ func NewTemplates(a *config.AppConfig) {
app = a app = a
} }
// AddDefaultData adds default template data
func AddDefaultData(td *models.TemplateData) *models.TemplateData {
return td
}
// RenderTemplate renders a HTML template file // 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 var tc map[string]*template.Template
if app.UseCache { if app.UseCache {
// get the template cache from the app config // 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 // Write to a buffer to make sure that the template can be read and
// written successfully // written successfully
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := t.Execute(buf, nil)
td = AddDefaultData(td)
err := t.Execute(buf, td)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }