<header>
<nav>
<a href="/"><h1>노마드 코인</h1></a>
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/add">Add</a>
</li>
</ul>
</nav>
<h1>{{.PageTitle}}</h1>
</header>
<main>
{{range .Blocks}}
<section>
<ul>
<li>{{.Data}}</li>
<li>{{.Hash}}</li>
{{if .PrevHash}}
<li>{{.PrevHash}}</li>
{{end}}
</ul>
</section>
{{end}}
</main>
{{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 "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>{{.PageTitle}}</h1>
</header>
{{end}}
{{define "footer"}}
<footer>© 2022</footer>
{{end}}
{{define "block"}}
<div>
<ul>
<li>{{.Data}}</li>
<li>{{.Hash}}</li>
{{if .PrevHash}}
<li>{{.PrevHash}}</li>
{{end}}
</ul>
</div>
<hr />
{{end}}
<!DOCTYPE html>
<html lang="en">
{{template "head"}}
<body>
{{template "header"}}
<main>
{{range .Blocks}}
{{template "block"}}
{{end}}
</main>
{{template "footer"}}
</body>
</html>
{{define "home"}}
<!DOCTYPE html>
<html lang="en">
{{template "head"}}
<body>
{{template "header"}}
<main>
{{range .Blocks}}
{{template "block"}}
{{end}}
</main>
{{template "footer"}}
</body>
</html>
{{end}}
{{define "add"}}
<!DOCTYPE html>
<html lang="en">
{{template "head"}}
<body>
{{template "header"}}
<main>
<form>
<input type="text" placeholder="Data for your block" required />
</form>
</main>
{{template "footer"}}
</body>
</html>
{{end}}
var templates *template.Template
const (
port string = ":4000"
templateDir string = "templates/"
)
func main() {
templates = template.Must(template.ParseGlob(templateDir + "pages/*.gohtml"))
http.HandleFunc("/", home)
fmt.Printf("Listening on http://localhost%s\n", port)
log.Fatal(http.ListenAndServe(port, nil))
}
templates = template.Must(template.ParseGlob(templateDir + "pages/*.gohtml"))
templates = template.Must(templates.ParseGlob())
templates = template.Must(template.ParseGlob(templateDir + "pages/*.gohtml"))
templates = template.Must(templates.ParseGlob(templateDir + "partials/*.gohtml"))
func home(rw http.ResponseWriter, r *http.Request) {
data := homeData{"Home", blockchain.GetBlockchain().AllBlocks()}
templates.ExecuteTemplate(rw, "home", data)
}
출처 : 노마드코더 노마드코인
[Nomad Coin] Explorer - #5.5 Refactoring (0) | 2022.07.18 |
---|---|
[Nomad Coin] Explorer - #5.4 Adding A Block (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 |
[Nomad Coin] Explorer - #5.0 Setup (0) | 2022.07.16 |
댓글 영역