diff --git a/conf.toml b/conf.toml index fac9bd3..488492c 100644 --- a/conf.toml +++ b/conf.toml @@ -1,4 +1,3 @@ extension = "md" notesdir = "saved_notes" templatesdir = "templates" -basetemplate = "base.tmpl.html" diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 4cb43f1..dce5fcf 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -11,7 +11,6 @@ type Config struct { Extension string NotesDir string TemplatesDir string - BaseTemplate string } var Conf Config diff --git a/internal/notes/views/views.go b/internal/notes/views/views.go index c76c731..de70e18 100644 --- a/internal/notes/views/views.go +++ b/internal/notes/views/views.go @@ -1,6 +1,7 @@ package views import ( + "html/template" "log" "net/http" "os" @@ -10,7 +11,6 @@ import ( urls "gitea.gwairfelin.com/max/gispatcho" "gitea.gwairfelin.com/max/gonotes/internal/conf" "gitea.gwairfelin.com/max/gonotes/internal/notes" - "gitea.gwairfelin.com/max/gonotes/internal/templ" ) var myurls urls.URLs @@ -37,9 +37,9 @@ func view(w http.ResponseWriter, r *http.Request) { return } - context := templ.Ctx{"note": note, "urlEdit": urlEdit} + context := map[string]any{"note": note, "urlEdit": urlEdit} note.Render() - err = templ.RenderTemplate(w, "view.tmpl.html", context) + err = renderTemplate(w, "view.html", context) if err != nil { log.Print(err.Error()) http.Error(w, "Couldn't load template", http.StatusInternalServerError) @@ -55,8 +55,8 @@ func edit(w http.ResponseWriter, r *http.Request) { } urlSave := myurls.Reverse("save", urls.Repl{"note": title}) - context := templ.Ctx{"note": note, "urlSave": urlSave} - err = templ.RenderTemplate(w, "edit.tmpl.html", context) + context := map[string]any{"note": note, "urlSave": urlSave} + err = renderTemplate(w, "edit.html", context) if err != nil { log.Print(err.Error()) http.Error(w, "Couldn't load template", http.StatusInternalServerError) @@ -89,10 +89,20 @@ func list(w http.ResponseWriter, r *http.Request) { } } - err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"titles": titles}) + err = renderTemplate(w, "list.tmpl.html", map[string]any{"titles": titles}) if err != nil { log.Print(err.Error()) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } } + +func renderTemplate(w http.ResponseWriter, tmpl string, context any) error { + files := []string{ + filepath.Join(conf.Conf.TemplatesDir, "base.tmpl.html"), + filepath.Join(conf.Conf.TemplatesDir, tmpl), + } + t, err := template.ParseFiles(files...) + t.ExecuteTemplate(w, "base", context) + return err +} diff --git a/internal/templ/templ.go b/internal/templ/templ.go deleted file mode 100644 index 0b9aa0d..0000000 --- a/internal/templ/templ.go +++ /dev/null @@ -1,29 +0,0 @@ -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 -} diff --git a/templates/edit.tmpl.html b/templates/edit.html similarity index 100% rename from templates/edit.tmpl.html rename to templates/edit.html diff --git a/templates/view.tmpl.html b/templates/view.html similarity index 100% rename from templates/view.tmpl.html rename to templates/view.html