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
|
|
@ -3,16 +3,26 @@ package templ
|
|||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||
)
|
||||
|
||||
type Ctx map[string]any
|
||||
|
||||
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||
files := []string{
|
||||
filepath.Join(conf.Conf.TemplatesDir, conf.Conf.BaseTemplate),
|
||||
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.ExecuteTemplate(w, "base", context)
|
||||
return err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue