From 3a0d758ef72b6cb3c1339a764ce663b2a0c88d53 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 20 Sep 2024 01:21:37 -0400 Subject: [PATCH] refactor: nest templates, extract base template --- routes.go | 12 +++++----- templates/about.html.tmpl | 27 ++++------------------ templates/base.html.tmpl | 22 ++++++++++++++++++ templates/index.html.tmpl | 37 ++++++++---------------------- templates/manage.html.tmpl | 47 ++++++++++++-------------------------- 5 files changed, 58 insertions(+), 87 deletions(-) create mode 100644 templates/base.html.tmpl diff --git a/routes.go b/routes.go index fd84f12..a4a04d4 100644 --- a/routes.go +++ b/routes.go @@ -26,8 +26,8 @@ func healthcheck(w http.ResponseWriter, r *http.Request) { // // Static content for the about page. func about(w http.ResponseWriter, r *http.Request) { - tmpl, _ := template.New("about.html.tmpl").ParseFiles("templates/about.html.tmpl") - tmpl.Execute(w, nil) + tmpl, _ := template.New("about").ParseFiles("templates/about.html.tmpl", "templates/base.html.tmpl") + tmpl.ExecuteTemplate(w, "base", nil) } // Feeds list @@ -41,8 +41,8 @@ func listContent(w http.ResponseWriter, r *http.Request) { links = append(links, formattedItems...) } - tmpl, _ := template.New("index.html.tmpl").ParseFiles("templates/index.html.tmpl") - tmpl.Execute(w, links) + tmpl, _ := template.New("base").ParseFiles("templates/index.html.tmpl", "templates/base.html.tmpl") + tmpl.ExecuteTemplate(w, "base", links) } // Manage content @@ -74,6 +74,6 @@ func manageContent(w http.ResponseWriter, r *http.Request) { Feeds map[string]string } - tmpl, _ := template.New("manage.html.tmpl").ParseFiles("templates/manage.html.tmpl") - tmpl.Execute(w, ManageTmplData{Feeds: allFeeds}) + tmpl, _ := template.New("manage").ParseFiles("templates/manage.html.tmpl", "templates/base.html.tmpl") + tmpl.ExecuteTemplate(w, "base", ManageTmplData{Feeds: allFeeds}) } diff --git a/templates/about.html.tmpl b/templates/about.html.tmpl index 5b1728d..3a8cb47 100644 --- a/templates/about.html.tmpl +++ b/templates/about.html.tmpl @@ -1,22 +1,5 @@ - - - ☕ Morning coffee - - - - -
-

☕ Morning coffee

- -
-
- Morning Coffee is a minimalist static-page RSS & interesting links reader written in Go. -
- - +{{ define "content" }} +
+ Morning Coffee is a minimalist static-page RSS & interesting links reader written in Go. +
+{{ end }} diff --git a/templates/base.html.tmpl b/templates/base.html.tmpl new file mode 100644 index 0000000..5968ab9 --- /dev/null +++ b/templates/base.html.tmpl @@ -0,0 +1,22 @@ +{{ define "base" }} + + + ☕ Morning coffee + + + + +
+

☕ Morning coffee

+ +
+ {{ template "content" . }} + + +{{ end }} diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index ffaf451..56fbd2c 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -1,27 +1,10 @@ - - - ☕ Morning coffee - - - - -
-

☕ Morning coffee

- -
- - - +{{ define "content" }} + +{{ end }} diff --git a/templates/manage.html.tmpl b/templates/manage.html.tmpl index d3190c6..e49f726 100644 --- a/templates/manage.html.tmpl +++ b/templates/manage.html.tmpl @@ -1,32 +1,15 @@ - - - ☕ Morning coffee - - - - -
-

☕ Morning coffee

- -
-
-
- - - -
-

Subscriptions

- -
- - +{{ define "content" }} +
+
+ + + +
+

Subscriptions

+ +
+{{ end }}