From e7e420cd4739a04778cbe947e5d907dc1ba127d4 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Tue, 28 Jan 2025 21:27:18 +0000 Subject: [PATCH] Add templ package to handle rendering of templates --- conf.toml | 1 + internal/conf/conf.go | 1 + internal/notes/views/views.go | 18 ++++-------------- internal/templ/templ.go | 19 +++++++++++++++++++ templates/{edit.html => edit.tmpl.html} | 0 templates/{view.html => view.tmpl.html} | 0 6 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 internal/templ/templ.go rename templates/{edit.html => edit.tmpl.html} (100%) rename templates/{view.html => view.tmpl.html} (100%) diff --git a/conf.toml b/conf.toml index 488492c..fac9bd3 100644 --- a/conf.toml +++ b/conf.toml @@ -1,3 +1,4 @@ extension = "md" notesdir = "saved_notes" templatesdir = "templates" +basetemplate = "base.tmpl.html" diff --git a/internal/conf/conf.go b/internal/conf/conf.go index dce5fcf..4cb43f1 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -11,6 +11,7 @@ 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 de70e18..7302dc2 100644 --- a/internal/notes/views/views.go +++ b/internal/notes/views/views.go @@ -1,7 +1,6 @@ package views import ( - "html/template" "log" "net/http" "os" @@ -11,6 +10,7 @@ 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 @@ -39,7 +39,7 @@ func view(w http.ResponseWriter, r *http.Request) { context := map[string]any{"note": note, "urlEdit": urlEdit} note.Render() - err = renderTemplate(w, "view.html", context) + err = templ.RenderTemplate(w, "view.tmpl.html", context) if err != nil { log.Print(err.Error()) http.Error(w, "Couldn't load template", http.StatusInternalServerError) @@ -56,7 +56,7 @@ func edit(w http.ResponseWriter, r *http.Request) { urlSave := myurls.Reverse("save", urls.Repl{"note": title}) context := map[string]any{"note": note, "urlSave": urlSave} - err = renderTemplate(w, "edit.html", context) + err = templ.RenderTemplate(w, "edit.tmpl.html", context) if err != nil { log.Print(err.Error()) http.Error(w, "Couldn't load template", http.StatusInternalServerError) @@ -89,20 +89,10 @@ func list(w http.ResponseWriter, r *http.Request) { } } - err = renderTemplate(w, "list.tmpl.html", map[string]any{"titles": titles}) + err = templ.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 new file mode 100644 index 0000000..0446a13 --- /dev/null +++ b/internal/templ/templ.go @@ -0,0 +1,19 @@ +package templ + +import ( + "html/template" + "net/http" + "path/filepath" + + "gitea.gwairfelin.com/max/gonotes/internal/conf" +) + +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), + } + t, err := template.ParseFiles(files...) + t.ExecuteTemplate(w, "base", context) + return err +} diff --git a/templates/edit.html b/templates/edit.tmpl.html similarity index 100% rename from templates/edit.html rename to templates/edit.tmpl.html diff --git a/templates/view.html b/templates/view.tmpl.html similarity index 100% rename from templates/view.html rename to templates/view.tmpl.html