Permalink
Browse files

refactor to leverage templates with define and block keywords, improv…

…ed style
  • Loading branch information...
johnpfeiffer committed May 10, 2018
1 parent c8636ee commit b6da397f89c8a6955a30e665ff1aa99be989e5cb
Showing with 35 additions and 30 deletions.
  1. +16 −0 base.tmpl
  2. +6 −0 components.tmpl
  3. +1 −1 hexcolors.go
  4. +8 −13 hexcolorstemplate.html
  5. +3 −0 index.html
  6. +1 −16 indextemplate.go
@@ -0,0 +1,16 @@
<html>
<head>
<style type="text/css">
{{block "style" .}}
body{
font-family: "Georgia";
font-size: 1.9em;
}
{{end}}
</style>
</head>
<body>
{{block "content" .}}
{{end}}
</body>
</html>
@@ -0,0 +1,6 @@
{{define "tablestyle"}}
table, th, td {
border: 2px solid black;
font-size: 2.5em;
}
{{end}}
@@ -13,7 +13,7 @@ type HexColors struct {

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

func hexController(w http.ResponseWriter, r *http.Request) {
@@ -1,16 +1,11 @@
<html>
<head><style>
table, th, td {
border: 2px solid black;
}
</style></head>
<body>
{{define "style"}}
{{template "tablestyle" .}}
{{end}}

{{define "content"}}
<table>
{{range .Colors}}
<tr>
<td>{{.}}</td><td style="background-color: {{.}}; ">__</td>
</tr>
<tr><td>{{.}}</td><td style="background-color: {{.}}; ">__</td></tr>
{{end}}
</table>
</body>
</html>
</table>
{{end}}
@@ -0,0 +1,3 @@
{{define "content"}}
hi , try <a href="/hexcolors">hexcolors</a>
{{end}}
@@ -7,21 +7,6 @@ type NoData struct{}

// GetIndexTemplate returns the index.html template https://golang.org/pkg/html/template/#Template
func GetIndexTemplate() *template.Template {
var indexTemplate = template.Must(template.New("index").Parse(`<html>
<head>
<title>Example Go Web App</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{
font-family: "Georgia";
font-size: 1.9em;
}
</style>
</head>
<body>
hi , try <a href="/hexcolors">hexcolors</a>
</body>
</html>
`))
indexTemplate := template.Must(template.ParseFiles("base.tmpl", "index.html"))
return indexTemplate
}

0 comments on commit b6da397

Please sign in to comment.