Skip to content

Commit b6da397

Browse files
committed
refactor to leverage templates with define and block keywords, improved style
1 parent c8636ee commit b6da397

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

base.tmpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<style type="text/css">
4+
{{block "style" .}}
5+
body{
6+
font-family: "Georgia";
7+
font-size: 1.9em;
8+
}
9+
{{end}}
10+
</style>
11+
</head>
12+
<body>
13+
{{block "content" .}}
14+
{{end}}
15+
</body>
16+
</html>

components.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{define "tablestyle"}}
2+
table, th, td {
3+
border: 2px solid black;
4+
font-size: 2.5em;
5+
}
6+
{{end}}

hexcolors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type HexColors struct {
1313

1414
// GetHexTemplate returns the parsed file as a template object
1515
func GetHexTemplate() *template.Template {
16-
return template.Must(template.ParseFiles("hexcolorstemplate.html"))
16+
return template.Must(template.ParseFiles("base.tmpl", "components.tmpl", "hexcolorstemplate.html"))
1717
}
1818

1919
func hexController(w http.ResponseWriter, r *http.Request) {

hexcolorstemplate.html

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
<html>
2-
<head><style>
3-
table, th, td {
4-
border: 2px solid black;
5-
}
6-
</style></head>
7-
<body>
1+
{{define "style"}}
2+
{{template "tablestyle" .}}
3+
{{end}}
4+
5+
{{define "content"}}
86
<table>
97
{{range .Colors}}
10-
<tr>
11-
<td>{{.}}</td><td style="background-color: {{.}}; ">__</td>
12-
</tr>
8+
<tr><td>{{.}}</td><td style="background-color: {{.}}; ">__</td></tr>
139
{{end}}
14-
</table>
15-
</body>
16-
</html>
10+
</table>
11+
{{end}}

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{define "content"}}
2+
hi , try <a href="/hexcolors">hexcolors</a>
3+
{{end}}

indextemplate.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@ type NoData struct{}
77

88
// GetIndexTemplate returns the index.html template https://golang.org/pkg/html/template/#Template
99
func GetIndexTemplate() *template.Template {
10-
var indexTemplate = template.Must(template.New("index").Parse(`<html>
11-
<head>
12-
<title>Example Go Web App</title>
13-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
14-
<style type="text/css">
15-
body{
16-
font-family: "Georgia";
17-
font-size: 1.9em;
18-
}
19-
</style>
20-
</head>
21-
<body>
22-
hi , try <a href="/hexcolors">hexcolors</a>
23-
</body>
24-
</html>
25-
`))
10+
indexTemplate := template.Must(template.ParseFiles("base.tmpl", "index.html"))
2611
return indexTemplate
2712
}

0 commit comments

Comments
 (0)