Do some better error handling in template rendering
This commit is contained in:
parent
e7e420cd47
commit
2ba0a0024e
2 changed files with 13 additions and 3 deletions
|
|
@ -37,7 +37,7 @@ func view(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
context := map[string]any{"note": note, "urlEdit": urlEdit}
|
context := templ.Ctx{"note": note, "urlEdit": urlEdit}
|
||||||
note.Render()
|
note.Render()
|
||||||
err = templ.RenderTemplate(w, "view.tmpl.html", context)
|
err = templ.RenderTemplate(w, "view.tmpl.html", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -55,7 +55,7 @@ func edit(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
urlSave := myurls.Reverse("save", urls.Repl{"note": title})
|
urlSave := myurls.Reverse("save", urls.Repl{"note": title})
|
||||||
context := map[string]any{"note": note, "urlSave": urlSave}
|
context := templ.Ctx{"note": note, "urlSave": urlSave}
|
||||||
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
|
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
|
|
@ -89,7 +89,7 @@ func list(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = templ.RenderTemplate(w, "list.tmpl.html", map[string]any{"titles": titles})
|
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"titles": titles})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,26 @@ package templ
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Ctx map[string]any
|
||||||
|
|
||||||
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||||
files := []string{
|
files := []string{
|
||||||
filepath.Join(conf.Conf.TemplatesDir, conf.Conf.BaseTemplate),
|
filepath.Join(conf.Conf.TemplatesDir, conf.Conf.BaseTemplate),
|
||||||
filepath.Join(conf.Conf.TemplatesDir, tmpl),
|
filepath.Join(conf.Conf.TemplatesDir, tmpl),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
_, err := os.Stat(f)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
t, err := template.ParseFiles(files...)
|
t, err := template.ParseFiles(files...)
|
||||||
t.ExecuteTemplate(w, "base", context)
|
t.ExecuteTemplate(w, "base", context)
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue