Skip to content
Permalink
Browse files

02-Template if-else

  • Loading branch information...
bonfy committed Sep 7, 2018
1 parent cca162a commit 9df5fb2f1f7d400513e3877aad0704a5e69366ac
Showing with 14 additions and 3 deletions.
  1. +8 −1 main.go
  2. +6 −2 templates/index.html
@@ -10,11 +10,18 @@ type User struct {
Username string
}

// IndexViewModel struct
type IndexViewModel struct {
Title string
User User
}

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
user := User{Username: "bonfy"}
v := IndexViewModel{Title: "Homepage", User: user}
tpl, _ := template.ParseFiles("templates/index.html")
tpl.Execute(w, &user)
tpl.Execute(w, &v)
})
http.ListenAndServe(":8888", nil)
}
@@ -1,8 +1,12 @@
<html>
<head>
<title>Home Page - Bonfy</title>
{{if .Title}}
<title>{{.Title}} - blog</title>
{{else}}
<title>Welcome to blog!</title>
{{end}}
</head>
<body>
<h1>Hello, {{.Username}}!</h1>
<h1>Hello, {{.User.Username}}!</h1>
</body>
</html>

0 comments on commit 9df5fb2

Please sign in to comment.
You can’t perform that action at this time.