rework template rendering, cache parsed templates

adapt error wrapping principle
This commit is contained in:
Johannes 2022-09-24 15:01:33 +02:00
parent 7a1448d6f9
commit f04396809d
21 changed files with 175 additions and 82 deletions

View file

@ -7,6 +7,7 @@ import (
"html/template"
"log"
"officeHours/models"
"officeHours/templating"
)
func (b *BaseHandler) GetTimetable(officeHours []models.OfficeHour) (timetable map[models.Date]map[int]models.OfficeHour, slots []int) {
@ -85,9 +86,11 @@ func (b *BaseHandler) printTimetable(timetable map[models.Date]map[int]models.Of
MinuteGranularity int
DeleteIcons bool
}{OfficeHour: current, MinuteGranularity: b.config.Date.MinuteGranularity, DeleteIcons: deleteIcons}
templateError := RawTemplates.ExecuteTemplate(&celldata, "td.html", data)
if templateError != nil {
log.Printf("Error executing template td.html: %s", templateError.Error())
err := templating.WriteTemplate(&celldata, "td", data)
if err != nil {
err = fmt.Errorf("writing table cell failed:\n%w", err)
log.Println(err.Error())
// TODO: better error wrapping up to top-level request handler
}
tableBody += celldata.String()
}
@ -120,9 +123,11 @@ func (b *BaseHandler) printTimetable(timetable map[models.Date]map[int]models.Of
slots[4],
template.HTML(tableBody),
}
templateError := RawTemplates.ExecuteTemplate(&table, "officeHourTable.html", tableData)
if templateError != nil {
log.Printf("Error executing template officeHourTable.html: %s", templateError.Error())
err := templating.WriteTemplate(&table, "officeHourTable", tableData)
if err != nil {
err = fmt.Errorf("writing table failed:\n%w", err)
log.Println(err.Error())
// TODO: better error wrapping up to top-level request handler
}
return template.HTML(table.String())
}