diff --git a/snippetbox/cmd/web/handlers.go b/snippetbox/cmd/web/handlers.go index debf020..294c2e4 100644 --- a/snippetbox/cmd/web/handlers.go +++ b/snippetbox/cmd/web/handlers.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "html/template" + "log" "net/http" "strconv" ) @@ -12,7 +14,26 @@ func home(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } - w.Write([]byte("Hello from snippetbox.chaosfem.tw")) + + files := []string{ + "./ui/html/base.tmpl", + "./ui/html/partials/nav.tmpl", + "./ui/html/pages/home.tmpl", + } + + ts, err := template.ParseFiles(files...) + if err != nil { + log.Print(err.Error()) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + + err = ts.ExecuteTemplate(w, "base", nil) + if err != nil { + log.Print(err.Error()) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } } // snippetView ... diff --git a/snippetbox/ui/html/base.tmpl b/snippetbox/ui/html/base.tmpl new file mode 100644 index 0000000..3277315 --- /dev/null +++ b/snippetbox/ui/html/base.tmpl @@ -0,0 +1,19 @@ +{{define "base"}} + + + + + {{template "title" .}} - Snippetbox + + +
+

Snippetbox

+
+ {{template "nav" .}} +
+ {{template "main" .}} +
+ + + +{{end}} \ No newline at end of file diff --git a/snippetbox/ui/html/pages/home.tmpl b/snippetbox/ui/html/pages/home.tmpl index fbf7233..534188a 100644 --- a/snippetbox/ui/html/pages/home.tmpl +++ b/snippetbox/ui/html/pages/home.tmpl @@ -1,17 +1,6 @@ - - - - - Home - Snippetbox - - -
-

Snippetbox

-
-
+{{define "title"}}Home{{end}} + +{{define "main"}}

Latest Snippets

leave me alone!

-
- - - +{{end}} diff --git a/snippetbox/ui/html/partials/nav.tmpl b/snippetbox/ui/html/partials/nav.tmpl new file mode 100644 index 0000000..f815ef4 --- /dev/null +++ b/snippetbox/ui/html/partials/nav.tmpl @@ -0,0 +1,5 @@ +{{define "nav"}} + +{{end}} \ No newline at end of file