Displaying server data in templates

This commit is contained in:
Beto
2024-02-26 21:00:14 -06:00
parent 6144cef1a2
commit 4709c1df35
2 changed files with 20 additions and 2 deletions

View File

@@ -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
View File

@@ -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)