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