From 1ffac114ff70b19bbf0a568c0f71523139b1ce69 Mon Sep 17 00:00:00 2001
From: Mihai Parparita
Date: Tue, 29 Jul 2014 22:29:06 -0700
Subject: [PATCH] Move digest rendering into its own template.
---
app/githop.go | 7 +++----
app/templates/digest.html | 25 +++++++++++++++++++++++++
app/templates/index-signed-out.html | 4 ++++
app/templates/index.html | 26 +++++---------------------
4 files changed, 37 insertions(+), 25 deletions(-)
create mode 100644 app/templates/digest.html
diff --git a/app/githop.go b/app/githop.go
index b6221be..3423821 100644
--- a/app/githop.go
+++ b/app/githop.go
@@ -52,8 +52,7 @@ func init() {
http.Handle("/", router)
}
-var indexTemplate = template.Must(template.ParseFiles("templates/index.html"))
-var indexSignedOutTemplate = template.Must(template.ParseFiles("templates/index-signed-out.html"))
+var templates = template.Must(template.ParseGlob("templates/*.html"))
func signInHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, githubOauthConfig.AuthCodeURL(""), http.StatusFound)
@@ -75,7 +74,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
var data = map[string]string{
"SignInUrl": signInUrl.String(),
}
- if err := indexSignedOutTemplate.Execute(w, data); err != nil {
+ if err := templates.ExecuteTemplate(w, "index-signed-out", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
@@ -105,7 +104,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
"SignOutUrl": signOutUrl.String(),
"Digest": digest,
}
- if err := indexTemplate.Execute(w, data); err != nil {
+ if err := templates.ExecuteTemplate(w, "index", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
diff --git a/app/templates/digest.html b/app/templates/digest.html
new file mode 100644
index 0000000..5a8636f
--- /dev/null
+++ b/app/templates/digest.html
@@ -0,0 +1,25 @@
+{{define "digest"}}
+
+{{.User.Login}}'s digest:
+
+ {{range $index, $repoDigest := .RepoDigests}}
+ -
+
+ {{.Repo.FullName}}
+
+
+ {{range .Commits }}
+ -
+
{{.SHA}}
+ -
+ {{.Commit.Author.Date}}
+
+ {{.Commit.Message}}
+
+ {{end}}
+
+
+ {{end}}
+
+
+{{end}}
diff --git a/app/templates/index-signed-out.html b/app/templates/index-signed-out.html
index 1c06116..03f6991 100644
--- a/app/templates/index-signed-out.html
+++ b/app/templates/index-signed-out.html
@@ -1,3 +1,5 @@
+{{define "index-signed-out"}}
+
@@ -13,3 +15,5 @@
+
+{{end}}
diff --git a/app/templates/index.html b/app/templates/index.html
index 0deb074..5cad7a0 100644
--- a/app/templates/index.html
+++ b/app/templates/index.html
@@ -1,3 +1,5 @@
+{{define "index"}}
+
@@ -14,26 +16,8 @@
- {{.Digest.User.Login}}'s digest:
-
- {{range $index, $repoDigest := .Digest.RepoDigests}}
- -
-
- {{.Repo.FullName}}
-
-
- {{range .Commits }}
- -
-
{{.SHA}}
- -
- {{.Commit.Author.Date}}
-
- {{.Commit.Message}}
-
- {{end}}
-
-
- {{end}}
-
+ {{template "digest" .Digest}}
+
+{{end}}