@@ -2,12 +2,15 @@ package main
2
2
3
3
import (
4
4
"database/sql"
5
+ "github.com/gin-contrib/multitemplate"
5
6
"html/template"
6
- "log"
7
+ // "log"
7
8
"net/http"
8
9
"os"
10
+ "path/filepath"
9
11
"sort"
10
12
"strconv"
13
+ "strings"
11
14
12
15
"github.com/gin-gonic/contrib/sessions"
13
16
"github.com/gin-gonic/contrib/static"
@@ -24,6 +27,25 @@ func getEnv(key, fallback string) string {
24
27
return fallback
25
28
}
26
29
30
+ func createRender () multitemplate.Renderer {
31
+ r := multitemplate .NewRenderer ()
32
+ pagesPath := "templates/includes"
33
+
34
+ layout := "templates/layouts/layout.tmpl"
35
+ files , err := os .ReadDir (pagesPath )
36
+ if err != nil {
37
+ panic (err )
38
+ }
39
+ funcs := template.FuncMap {"indexPlus1" : func (i int ) int { return i + 1 }}
40
+
41
+ for _ , file := range files {
42
+ fileNameWithoutExt := strings .TrimSuffix (file .Name (), filepath .Ext (file .Name ()))
43
+ //fileName := file.Name()
44
+ r .AddFromFilesFuncs (fileNameWithoutExt , funcs , layout , pagesPath + "/" + file .Name ())
45
+ }
46
+ return r
47
+ }
48
+
27
49
func main () {
28
50
// database setting
29
51
user := getEnv ("ISHOCON2_DB_USER" , "ishocon" )
@@ -35,7 +57,10 @@ func main() {
35
57
gin .SetMode (gin .DebugMode )
36
58
r := gin .Default ()
37
59
r .Use (static .Serve ("/css" , static .LocalFile ("public/css" , true )))
38
- layout := "templates/layout.tmpl"
60
+
61
+ r .LoadHTMLFiles ("templates/**/*.tmpl" )
62
+ //r.HTMLRender = createRender()
63
+ r .HTMLRender = createRender ()
39
64
40
65
// session store
41
66
store := sessions .NewCookieStore ([]byte ("mysession" ))
@@ -67,9 +92,6 @@ func main() {
67
92
// Sort partyNames
68
93
sort .Strings (partyNames )
69
94
70
- // log partyNames value for debug
71
- log .Println (partyNames )
72
-
73
95
//partyNames := getAllPartyName()
74
96
partyResultMap := map [string ]int {}
75
97
for _ , name := range partyNames {
@@ -100,9 +122,7 @@ func main() {
100
122
}
101
123
}
102
124
103
- funcs := template.FuncMap {"indexPlus1" : func (i int ) int { return i + 1 }}
104
- r .SetHTMLTemplate (template .Must (template .New ("main" ).Funcs (funcs ).ParseFiles (layout , "templates/index.tmpl" )))
105
- c .HTML (http .StatusOK , "base" , gin.H {
125
+ c .HTML (http .StatusOK , "index" , gin.H {
106
126
"candidates" : candidates ,
107
127
"parties" : partyResults ,
108
128
"sexRatio" : sexRatio ,
@@ -129,8 +149,7 @@ func main() {
129
149
candidateIDs := []int {candidateID }
130
150
keywords := getVoiceOfSupporter (candidateIDs )
131
151
132
- r .SetHTMLTemplate (template .Must (template .ParseFiles (layout , "templates/candidate.tmpl" )))
133
- c .HTML (http .StatusOK , "base" , gin.H {
152
+ c .HTML (http .StatusOK , "candidate" , gin.H {
134
153
"candidate" : candidate ,
135
154
"votes" : votes ,
136
155
"keywords" : keywords ,
@@ -161,8 +180,7 @@ func main() {
161
180
}
162
181
keywords := getVoiceOfSupporter (candidateIDs )
163
182
164
- r .SetHTMLTemplate (template .Must (template .ParseFiles (layout , "templates/political_party.tmpl" )))
165
- c .HTML (http .StatusOK , "base" , gin.H {
183
+ c .HTML (http .StatusOK , "political_party" , gin.H {
166
184
"politicalParty" : partyName ,
167
185
"votes" : votes ,
168
186
"candidates" : candidatesByParty ,
@@ -172,8 +190,7 @@ func main() {
172
190
173
191
// GET /vote
174
192
r .GET ("/vote" , func (c * gin.Context ) {
175
- r .SetHTMLTemplate (template .Must (template .ParseFiles (layout , "templates/vote.tmpl" )))
176
- c .HTML (http .StatusOK , "base" , gin.H {
193
+ c .HTML (http .StatusOK , "vote" , gin.H {
177
194
"candidates" : candidates ,
178
195
"message" : "" ,
179
196
})
@@ -194,7 +211,6 @@ func main() {
194
211
voteCount , _ := strconv .Atoi (c .PostForm ("vote_count" ))
195
212
196
213
var message string
197
- r .SetHTMLTemplate (template .Must (template .ParseFiles (layout , "templates/vote.tmpl" )))
198
214
if userErr != nil {
199
215
message = "個人情報に誤りがあります"
200
216
} else if user .Votes < voteCount + votedCount {
@@ -213,7 +229,7 @@ func main() {
213
229
createVote (user .ID , candidate .ID , c .PostForm ("keyword" ), voteCount )
214
230
message = "投票に成功しました"
215
231
}
216
- c .HTML (http .StatusOK , "base " , gin.H {
232
+ c .HTML (http .StatusOK , "vote " , gin.H {
217
233
"candidates" : candidates ,
218
234
"message" : message ,
219
235
})
0 commit comments