From 34fe9b499259a2ac814aca2292f49fbf0f46f0ae Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Mon, 27 May 2024 21:16:53 -0400 Subject: [PATCH] seperate templating for routes --- src/routes/example.go | 13 ++----------- src/routes/root.go | 23 +++++------------------ src/routes/template.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 src/routes/template.go diff --git a/src/routes/example.go b/src/routes/example.go index 4ac2f69..090389c 100644 --- a/src/routes/example.go +++ b/src/routes/example.go @@ -1,7 +1,6 @@ package routes import ( - "html/template" "net/http" ) @@ -10,14 +9,6 @@ func ExampleHandler(w http.ResponseWriter, r *http.Request) { Title: "Example Page", } - tmpl, err := template.ParseFiles("web/views/#layout.html", "web/views/example.html", "web/views/#header.html", "web/views/#footer.html") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - err = tmpl.ExecuteTemplate(w, "layout", data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } + // Call renderTemplate with the specific template for this route + renderTemplate(w, data, "web/views/example.html") } diff --git a/src/routes/root.go b/src/routes/root.go index ad7dd9b..3627225 100644 --- a/src/routes/root.go +++ b/src/routes/root.go @@ -1,27 +1,14 @@ package routes import ( - "html/template" "net/http" ) -type PageData struct { - Title string -} - func RootHandler(w http.ResponseWriter, r *http.Request) { - data := PageData{ - Title: "Home Page", - } + data := PageData{ + Title: "Home Page", + } - tmpl, err := template.ParseFiles("web/views/#layout.html", "web/views/index.html", "web/views/#header.html", "web/views/#footer.html") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - err = tmpl.ExecuteTemplate(w, "layout", data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } + // Call renderTemplate with the specific template for this route + renderTemplate(w, data, "web/views/index.html") } diff --git a/src/routes/template.go b/src/routes/template.go new file mode 100644 index 0000000..1d45647 --- /dev/null +++ b/src/routes/template.go @@ -0,0 +1,35 @@ +package routes + +import ( + "html/template" + "net/http" +) + +type PageData struct { + Title string +} + +func renderTemplate(w http.ResponseWriter, data PageData, specificTemplate string) { + // List of common templates + commonTemplates := []string{ + "web/views/#layout.html", + "web/views/#header.html", + "web/views/#footer.html", + } + + // Append the specific template for the route + templates := append(commonTemplates, specificTemplate) + + // Parse all templates + tmpl, err := template.ParseFiles(templates...) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // Execute the "layout" template + err = tmpl.ExecuteTemplate(w, "layout", data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +}