From 4709c1df35ac58941610665653c2630b53718bb2 Mon Sep 17 00:00:00 2001 From: Beto Date: Mon, 26 Feb 2024 21:00:14 -0600 Subject: [PATCH] Displaying server data in templates --- index.html | 8 +++++++- main.go | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 522de3a..9451cbf 100644 --- a/index.html +++ b/index.html @@ -10,6 +10,12 @@ Document -

Hello-Bootstrap

+

Hello-Bootstrap

+ + {{ range .Films }} + +

{{ .Title }} - {{ .Director }}

+ + {{ end }} \ No newline at end of file diff --git a/main.go b/main.go index f680b54..a0a5fb4 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,24 @@ import ( "text/template" ) +type Film struct { + Title string + Director string +} + func main() { fmt.Println("hello world") h1 := func(w http.ResponseWriter, r *http.Request) { 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)