/
inherit.go
46 lines (38 loc) · 923 Bytes
/
inherit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"html/template"
"net/http"
)
var t map[string]*template.Template
func init() {
t = make(map[string]*template.Template)
temp := template.Must(template.ParseFiles("base.html", "user.html"))
t["user.html"] = temp
temp = template.Must(template.ParseFiles("base.html", "page.html"))
t["page.html"] = temp
}
type Page struct {
Title, Content string
}
type User struct {
Username, Name string
}
func displayPage(w http.ResponseWriter, r *http.Request) {
p := &Page{
Title: "An Example",
Content: "Have fun stormin’ da castle.",
}
t["page.html"].ExecuteTemplate(w, "base", p)
}
func displayUser(w http.ResponseWriter, r *http.Request) {
u := &User{
Username: "swordsmith",
Name: "Inigo Montoya",
}
t["user.html"].ExecuteTemplate(w, "base", u)
}
func main() {
http.HandleFunc("/user", displayUser)
http.HandleFunc("/", displayPage)
http.ListenAndServe(":8080", nil)
}