{{define "home"}}
<!DOCTYPE html>
<html lang="en">
{{template "head"}}
<body>
{{template "header" .PageTitle}}
<main>
{{range .Blocks}}
{{template "block"}}
{{end}}
</main>
{{template "footer"}}
</body>
</html>
{{end}}
{{define "header"}}
<header>
<nav>
<a href="/"><h1>노마드 코인</h1></a>
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/add">Add</a>
</li>
</ul>
</nav>
<h1>{{.}}</h1>
</header>
{{end}}
{{define "head"}}
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/mvp.css">
<title>{{.}} | 노마드 코인</title>
</head>
{{end}}
{{define "home"}}
<!DOCTYPE html>
<html lang="en">
{{template "head" .PageTitle}}
<body>
{{template "header" .PageTitle}}
<main>
{{range .Blocks}}
{{template "block"}}
{{end}}
</main>
{{template "footer"}}
</body>
</html>
{{end}}
func main() {
templates = template.Must(template.ParseGlob(templateDir + "pages/*.gohtml"))
templates = template.Must(templates.ParseGlob(templateDir + "partials/*.gohtml"))
http.HandleFunc("/", home)
http.HandleFunc("/add", add)
fmt.Printf("Listening on http://localhost%s\n", port)
log.Fatal(http.ListenAndServe(port, nil))
}
func add(rw http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(rw, "add", nil)
}
{{define "add"}}
<!DOCTYPE html>
<html lang="en">
{{template "head" "Add"}}
<body>
{{template "header" "Add"}}
<main>
<form>
<input type="text" placeholder="Data for your block" required />
</form>
</main>
{{template "footer"}}
</body>
</html>
{{end}}
{{define "add"}}
<!DOCTYPE html>
<html lang="en">
{{template "head" "Add"}}
<body>
{{template "header" "Add"}}
<main>
<form method="POST" action="/add">
<input type="text" placeholder="Data for your block" required name="blockData" />
<button>Add Block</button>
</form>
</main>
{{template "footer"}}
</body>
</html>
{{end}}
func add(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
templates.ExecuteTemplate(rw, "add", nil)
case "POST":
r.ParseForm()
data := r.Form.Get("blockData")
blockchain.GetBlockchain().AddBlock(data)
}
}
func Redirect(w ResponseWriter, r *Request, url string, code int)
func add(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
templates.ExecuteTemplate(rw, "add", nil)
case "POST":
r.ParseForm()
data := r.Form.Get("blockData")
blockchain.GetBlockchain().AddBlock(data)
http.Redirect(rw, r, "/", http.StatusPermanentRedirect)
}
}
강의 : 노마드코더 노마드코인
[Nomad Coin] Explorer - #5.6 Recap (0) | 2022.07.18 |
---|---|
[Nomad Coin] Explorer - #5.5 Refactoring (0) | 2022.07.18 |
[Nomad Coin] Explorer - #5.3 Using Partials (0) | 2022.07.18 |
[Nomad Coin] Explorer - #5.2 Rendering Templates (0) | 2022.07.17 |
[Nomad Coin] Explorer - #5.1 Rendering Templates (0) | 2022.07.17 |
댓글 영역