Cleaning up our code

This commit is contained in:
2024-07-07 22:37:48 +02:00
parent 44385fbbce
commit 18c82ba19f
6 changed files with 22 additions and 19 deletions

View File

@ -21,8 +21,8 @@ var (
pathToTemplates = "./templates"
)
// NewTemplates sets the config for the template package
func NewTemplates(a *config.AppConfig) {
// NewRenderer sets the config for the template package
func NewRenderer(a *config.AppConfig) {
app = a
}
@ -35,8 +35,8 @@ func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateDa
return td
}
// RenderTemplate renders a HTML template file
func RenderTemplate(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) error {
// Template renders a HTML template file
func Template(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) error {
var tc map[string]*template.Template
if app.UseCache {
// get the template cache from the app config

View File

@ -39,12 +39,12 @@ func TestRenderTemplate(t *testing.T) {
var ww myWriter
err = RenderTemplate(&ww, r, "home.page.tmpl", &models.TemplateData{})
err = Template(&ww, r, "home.page.tmpl", &models.TemplateData{})
if err != nil {
t.Error("error writiing template to browser")
}
err = RenderTemplate(&ww, r, "non-existent.page.html", &models.TemplateData{})
err = Template(&ww, r, "non-existent.page.html", &models.TemplateData{})
if err == nil {
t.Error("rendered template that doesn't exist")
}
@ -64,7 +64,7 @@ func getSession() (*http.Request, error) {
}
func TestNewTemplate(t *testing.T) {
NewTemplates(app)
NewRenderer(app)
}
func TestCreateTemplateCache(t *testing.T) {