Displaying server data in templates
This commit is contained in:
@@ -10,6 +10,12 @@
|
|||||||
<title>Document</title>
|
<title>Document</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Hello-Bootstrap</h1>
|
<h1>Hello-Bootstrap</h1>
|
||||||
|
|
||||||
|
{{ range .Films }}
|
||||||
|
|
||||||
|
<p>{{ .Title }} - {{ .Director }}</p>
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
14
main.go
14
main.go
@@ -7,12 +7,24 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Film struct {
|
||||||
|
Title string
|
||||||
|
Director string
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("hello world")
|
fmt.Println("hello world")
|
||||||
|
|
||||||
h1 := func(w http.ResponseWriter, r *http.Request) {
|
h1 := func(w http.ResponseWriter, r *http.Request) {
|
||||||
tmpl := template.Must(template.ParseFiles("index.html"))
|
tmpl := template.Must(template.ParseFiles("index.html"))
|
||||||
tmpl.Execute(w, nil)
|
films := map[string][]Film{
|
||||||
|
"Films": {
|
||||||
|
{Title: "The Goodfather", Director: "Francis Ford Coppola"},
|
||||||
|
{Title: "Blade Runner", Director: "Ridley Scott"},
|
||||||
|
{Title: "The Thing", Director: "John Carpenter"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tmpl.Execute(w, films)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", h1)
|
http.HandleFunc("/", h1)
|
||||||
|
|||||||
Reference in New Issue
Block a user