log template rendering errors; some docstrings
This commit is contained in:
parent
d60b715d96
commit
a755a0b3e9
1 changed files with 18 additions and 5 deletions
|
@ -3,6 +3,7 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"officeHours/models"
|
"officeHours/models"
|
||||||
"os"
|
"os"
|
||||||
|
@ -14,33 +15,45 @@ var funcs = template.FuncMap{
|
||||||
}
|
}
|
||||||
var baseTemplate = template.Must(template.ParseFiles("templates/base.html")).New("base.html").Funcs(funcs)
|
var baseTemplate = template.Must(template.ParseFiles("templates/base.html")).New("base.html").Funcs(funcs)
|
||||||
|
|
||||||
|
// Execute a rendered full HTML page.
|
||||||
|
//
|
||||||
|
// If you just want to fill a template snippet, use the RawTemplates object.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
// - name is the name of the template file inside templates/, without .html suffix.
|
||||||
|
// - data is passed through to the template
|
||||||
func (b *BaseHandler) serveTemplate(w http.ResponseWriter, name string, data any) {
|
func (b *BaseHandler) serveTemplate(w http.ResponseWriter, name string, data any) {
|
||||||
full_name := "templates/" + name + ".html"
|
full_name := "templates/" + name + ".html"
|
||||||
// check that template exists
|
// check that template exists
|
||||||
info, err := os.Stat(full_name)
|
info, err := os.Stat(full_name)
|
||||||
if (err != nil && os.IsNotExist(err)) || info.IsDir() {
|
if (err != nil && os.IsNotExist(err)) || info.IsDir() {
|
||||||
|
errMsg := fmt.Sprintf("Template %s nicht gefunden", full_name)
|
||||||
|
log.Println(errMsg)
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
w.Write([]byte("404: Template nicht gefunden."))
|
w.Write([]byte(errMsg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := baseTemplate.ParseFiles(full_name)
|
tmpl, err := baseTemplate.ParseFiles(full_name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: log.Printf
|
errMsg := fmt.Sprintf("Template %s konnte nicht geparst werden : %s", full_name, err.Error())
|
||||||
|
log.Println(errMsg)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(fmt.Sprintf("500: Template "+full_name+" konnte nicht geparst werden : %s", err.Error())))
|
w.Write([]byte(errMsg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// TODO: cache templates in parsed state, but not executed
|
// TODO: cache templates in parsed state, but not executed
|
||||||
err = template.Must(tmpl.Clone()).Execute(w, data)
|
err = template.Must(tmpl.Clone()).Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: log.Printf
|
errMsg := fmt.Sprintf("Template %s konnte nicht ausgeführt werden : %s", full_name, err.Error())
|
||||||
|
log.Println(errMsg)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(fmt.Sprintf("500: Template "+full_name+" konnte nicht ausgeführt werden : %s", err.Error())))
|
w.Write([]byte(errMsg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// standalone templates that should not be wrapped into the base.html
|
||||||
var RawTemplates = template.Must(template.New("").Funcs(funcs).ParseFiles(
|
var RawTemplates = template.Must(template.New("").Funcs(funcs).ParseFiles(
|
||||||
"templates/confirmationMail",
|
"templates/confirmationMail",
|
||||||
"templates/td.html",
|
"templates/td.html",
|
||||||
|
|
Loading…
Reference in a new issue