From 892d28f58b4760ac8296c872c81275fab4cfa719 Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Fri, 26 Nov 2021 15:29:13 +0900 Subject: [PATCH] move header and footer into base template --- assets/templates/404.html | 82 +------------------- assets/templates/about.html | 85 +------------------- assets/templates/base.html | 125 ++++++++++++++++++++++++++++++ assets/templates/footer.html | 30 ------- assets/templates/high_scores.html | 85 +------------------- assets/templates/home.html | 85 +------------------- assets/templates/report.html | 29 ++++++- assets/templates/supporters.html | 85 +------------------- handlers/about.go | 6 +- handlers/error.go | 4 +- handlers/high_scores.go | 6 +- handlers/home.go | 6 +- handlers/report.go | 6 +- handlers/supporters.go | 6 +- handlers/templates.go | 30 +++---- 15 files changed, 203 insertions(+), 467 deletions(-) create mode 100644 assets/templates/base.html delete mode 100644 assets/templates/footer.html diff --git a/assets/templates/404.html b/assets/templates/404.html index 0ad50ad..877dbdb 100644 --- a/assets/templates/404.html +++ b/assets/templates/404.html @@ -1,58 +1,4 @@ - - - - - - - Go Report Card | Go project code quality report cards - - - - - -
-
- - - - - - - - - - - -
- - High Scores - - - GitHub - - - About - -
-
-
+[[ define "content" ]]
@@ -67,7 +13,6 @@
- [[ template "footer" . ]] - - - - +[[ end ]] diff --git a/assets/templates/about.html b/assets/templates/about.html index 2388835..4df09c9 100644 --- a/assets/templates/about.html +++ b/assets/templates/about.html @@ -1,61 +1,4 @@ - - - - - - - Go Report Card | Go project code quality report cards - - - - - -
-
- - - - - - - - - - - -
- - High Scores - - - GitHub - - - Supporters - - - About - -
-
-
+[[ define "content" ]]

About

@@ -66,28 +9,4 @@

Go Report Card relies on the excellent Go Meta Linter tool.

- [[ template "footer" . ]] - - - - +[[ end ]] diff --git a/assets/templates/base.html b/assets/templates/base.html new file mode 100644 index 0000000..6725c50 --- /dev/null +++ b/assets/templates/base.html @@ -0,0 +1,125 @@ +[[ define "base" ]] + + + + + + + Go Report Card | Go project code quality report cards + + + + + + + +
+
+
+
+ + + + + + + + + + + +
+ + High Scores + + + GitHub + + + Supporters + + + Patreon + + + About + +
+
+
+ +[[ template "content" . ]] + + + + + + + + + +[[ end ]] diff --git a/assets/templates/footer.html b/assets/templates/footer.html deleted file mode 100644 index e5544ac..0000000 --- a/assets/templates/footer.html +++ /dev/null @@ -1,30 +0,0 @@ -[[ define "footer" ]] - -[[ end ]] diff --git a/assets/templates/high_scores.html b/assets/templates/high_scores.html index 016de85..bd002f5 100644 --- a/assets/templates/high_scores.html +++ b/assets/templates/high_scores.html @@ -1,61 +1,4 @@ - - - - - - - Go Report Card | Go project code quality report cards - - - - - -
-
- - - - - - - - - - - -
- - High Scores - - - GitHub - - - Supporters - - - About - -
-
-
+[[ define "content" ]]

High Scores

@@ -85,28 +28,4 @@

- [[ template "footer" . ]] - - - - +[[ end ]] diff --git a/assets/templates/home.html b/assets/templates/home.html index d419ad9..45795ef 100644 --- a/assets/templates/home.html +++ b/assets/templates/home.html @@ -1,62 +1,4 @@ - - - - - - - Go Report Card | Go project code quality report cards - - - - - -
-
-
-
- - - - - - - - - - - -
- - High Scores - - - GitHub - - - Supporters - - - Patreon - - - About - -
-
-
+[[ define "content" ]]
@@ -132,8 +74,6 @@
- [[ template "footer" . ]] - - - +[[ end ]] diff --git a/assets/templates/report.html b/assets/templates/report.html index 29d8357..eacde81 100644 --- a/assets/templates/report.html +++ b/assets/templates/report.html @@ -87,7 +87,34 @@ - [[ template "footer" . ]] + diff --git a/assets/templates/supporters.html b/assets/templates/supporters.html index 5f0b768..47174fc 100644 --- a/assets/templates/supporters.html +++ b/assets/templates/supporters.html @@ -1,61 +1,4 @@ - - - - - - - Go Report Card | Go project code quality report cards - - - - - -
-
- - - - - - - - - - - -
- - High Scores - - - GitHub - - - Supporters - - - About - -
-
-
+[[ define "content" ]]

Supporters

@@ -68,28 +11,4 @@
- [[ template "footer" . ]] - - - - +[[ end ]] diff --git a/handlers/about.go b/handlers/about.go index 41a0ad3..b35efe5 100644 --- a/handlers/about.go +++ b/handlers/about.go @@ -14,7 +14,9 @@ func (gh *GRCHandler) AboutHandler(w http.ResponseWriter, r *http.Request) { return } - t.Execute(w, map[string]interface{}{ + if err := t.ExecuteTemplate(w, "base", map[string]interface{}{ "google_analytics_key": googleAnalyticsKey, - }) + }); err != nil { + log.Println("ERROR:", err) + } } diff --git a/handlers/error.go b/handlers/error.go index 4e5819a..07f63c4 100644 --- a/handlers/error.go +++ b/handlers/error.go @@ -15,6 +15,8 @@ func (gh *GRCHandler) errorHandler(w http.ResponseWriter, r *http.Request, statu return } - t.Execute(w, nil) + if err := t.ExecuteTemplate(w, "base", nil); err != nil { + log.Println("ERROR:", err) + } } } diff --git a/handlers/high_scores.go b/handlers/high_scores.go index 587f5ca..99c4c98 100644 --- a/handlers/high_scores.go +++ b/handlers/high_scores.go @@ -68,9 +68,11 @@ func (gh *GRCHandler) HighScoresHandler(w http.ResponseWriter, r *http.Request, sortedScores[len(sortedScores)-i-1] = heap.Pop(scores).(scoreItem) } - t.Execute(w, map[string]interface{}{ + if err := t.ExecuteTemplate(w, "base", map[string]interface{}{ "HighScores": sortedScores, "Count": humanize.Comma(int64(count)), "google_analytics_key": googleAnalyticsKey, - }) + }); err != nil { + log.Println("ERROR:", err) + } } diff --git a/handlers/home.go b/handlers/home.go index 6dfbf18..e93246a 100644 --- a/handlers/home.go +++ b/handlers/home.go @@ -69,10 +69,12 @@ func (gh *GRCHandler) HomeHandler(w http.ResponseWriter, r *http.Request, db *ba return } - t.Execute(w, map[string]interface{}{ + if err := t.ExecuteTemplate(w, "base", map[string]interface{}{ "Recent": recentRepos, "google_analytics_key": googleAnalyticsKey, - }) + }); err != nil { + log.Println(err) + } return } diff --git a/handlers/report.go b/handlers/report.go index 9a71d97..3139825 100644 --- a/handlers/report.go +++ b/handlers/report.go @@ -42,11 +42,13 @@ func (gh *GRCHandler) ReportHandler(w http.ResponseWriter, r *http.Request, db * return } - t.Execute(w, map[string]interface{}{ + if err := t.Execute(w, map[string]interface{}{ "repo": repo, "response": string(respBytes), "loading": needToLoad, "domain": domain, "google_analytics_key": googleAnalyticsKey, - }) + }); err != nil { + log.Println("ERROR:", err) + } } diff --git a/handlers/supporters.go b/handlers/supporters.go index ca1338f..88b75e7 100644 --- a/handlers/supporters.go +++ b/handlers/supporters.go @@ -14,7 +14,9 @@ func (gh *GRCHandler) SupportersHandler(w http.ResponseWriter, r *http.Request) return } - t.Execute(w, map[string]interface{}{ + if err := t.ExecuteTemplate(w, "base", map[string]interface{}{ "google_analytics_key": googleAnalyticsKey, - }) + }); err != nil { + log.Println("ERROR:", err) + } } diff --git a/handlers/templates.go b/handlers/templates.go index 620c889..7c6cfa0 100644 --- a/handlers/templates.go +++ b/handlers/templates.go @@ -27,18 +27,6 @@ func (gh *GRCHandler) loadTemplate(name string) (*template.Template, error) { return nil, err } - footer, err := gh.AssetsFS.Open("/templates/footer.html") - if err != nil { - return nil, err - } - - defer footer.Close() - - footerContents, err := ioutil.ReadAll(footer) - if err != nil { - return nil, err - } - tpl, err := template.New(name).Delims("[[", "]]").Funcs(template.FuncMap{ "add": add, "formatScore": formatScore, @@ -47,5 +35,21 @@ func (gh *GRCHandler) loadTemplate(name string) (*template.Template, error) { return nil, err } - return tpl.Parse(string(footerContents)) + if name == "/templates/report.html" { + return tpl, nil + } + + base, err := gh.AssetsFS.Open("/templates/base.html") + if err != nil { + return nil, err + } + + defer base.Close() + + baseContents, err := ioutil.ReadAll(base) + if err != nil { + return nil, err + } + + return tpl.Parse(string(baseContents)) }