From 2e88f6f9d49dfaba9f0fb780aa7cc11abfb6ba37 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Fri, 27 Feb 2026 15:05:47 +0300 Subject: [PATCH] ch2.7 --- cmd/web/handlers.go | 19 ++++++++++++++++++- ui/html/base.tmpl | 19 +++++++++++++++++++ ui/html/pages/home.tmpl | 6 ++++++ ui/html/partials/nav.tmpl | 5 +++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 ui/html/base.tmpl create mode 100644 ui/html/pages/home.tmpl create mode 100644 ui/html/partials/nav.tmpl diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 9de0073..ca36a67 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "html/template" + "log" "net/http" "strconv" ) @@ -11,7 +13,22 @@ func home(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } - w.Write([]byte("Hello from Snippetbox")) + 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) + } } func snippetView(w http.ResponseWriter, r *http.Request) { diff --git a/ui/html/base.tmpl b/ui/html/base.tmpl new file mode 100644 index 0000000..a22d752 --- /dev/null +++ b/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/ui/html/pages/home.tmpl b/ui/html/pages/home.tmpl new file mode 100644 index 0000000..968e57e --- /dev/null +++ b/ui/html/pages/home.tmpl @@ -0,0 +1,6 @@ +{{define "title"}}Home{{end}} + +{{define "main"}} +

Latest Snippets

+

There's nothing to see here yet!

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