Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
initial commit
base.html | 25 +++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ index.html | 11 +++++++++++ main.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.html | 2 ++
diff --git a/base.html b/base.html new file mode 100644 index 0000000000000000000000000000000000000000..e5854aa89e44620f75d873b235c2edaffd3260e1 --- /dev/null +++ b/base.html @@ -0,0 +1,25 @@ +{{.Title}} +{{ printf "%#v" . }} + +{{define "base"}} + <!doctype html> + <html lang="en"> + <head> + <title> + {{block "title" .}} + Go test + {{end}} + </title> + </head> + <body> + {{.Title}} + {{ printf "%#v" . }} + + {{block "body" .}} + {{end}} + <footer> + Footer + </footer> + </body> + </html> +{{end}} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..9dcb66efacfb5a8eb5030407334213e6573842a7 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.eletrotupi.com/go-tmpl-demo + +go 1.19 + +require github.com/go-chi/chi v1.5.4 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..874ed9a397d891e96033f33aadfc719831640935 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs= +github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg= diff --git a/index.html b/index.html new file mode 100644 index 0000000000000000000000000000000000000000..ed8bca602467c8f564c7578c5b737f1dd8730eb9 --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ +{{template "base"}} + {{define "title"}} + Test - Home + {{end}} + +{{define "body"}} + <main> + {{.Title}} + <h1>Hello, {{ printf "%#v" . }}</h1> + </main> +{{end}} diff --git a/main.go b/main.go new file mode 100644 index 0000000000000000000000000000000000000000..567660ffc6a1d6441d947e1eefbaf0181b92fafb --- /dev/null +++ b/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "net/http" + "log" + "html/template" + + "github.com/go-chi/chi" +) + +type WebCtx struct { + Title string +} + +func main() { + router := chi.NewRouter() + + router.Get("/", func(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.ParseFiles("base.html", "index.html") + if err != nil { + log.Fatalf("Can't parse files due to %v", err) + return + } + + err = tmpl.ExecuteTemplate(w, "index.html", &WebCtx{ + Title: "Test", + }) + + if err != nil { + log.Fatalf("Can't execute template due to %v", err) + return + } + }) + + router.Get("/working", func(w http.ResponseWriter, r *http.Request) { + tmpl, err := template.ParseFiles("test.html") + if err != nil { + log.Fatalf("Can't parse files due to %v", err) + return + } + + err = tmpl.ExecuteTemplate(w, "test.html", &WebCtx{ + Title: "Test", + }) + + if err != nil { + log.Fatalf("Can't execute template due to %v", err) + return + } + }) + + log.Printf("Running on :2000") + http.ListenAndServe(":2000", router) +} diff --git a/test.html b/test.html new file mode 100644 index 0000000000000000000000000000000000000000..53a2f57959c208b8e154acc09d6c29f51415fee8 --- /dev/null +++ b/test.html @@ -0,0 +1,2 @@ +{{.Title}} +{{ printf "%#v" . }}