From 733326ad022573c7f12df845b0b3ed83499e906e Mon Sep 17 00:00:00 2001 From: Anthony Bullard <1380687+gamebox@users.noreply.github.com> Date: Sun, 7 Jan 2024 16:35:47 -0600 Subject: [PATCH] example: add bare-bones HTMX example (#15) Closes #8 Obviously it's the simplest, silliest example possible, but we can build upon it as we go along. --- README.md | 4 +++ htmx-example/main.go | 33 +++++++++++++++++++++++ htmx-example/templates/base.html.gwirl | 12 +++++++++ htmx-example/templates/counter.html.gwirl | 5 ++++ htmx-example/templates/index.html.gwirl | 10 +++++++ 5 files changed, 64 insertions(+) create mode 100644 htmx-example/main.go create mode 100644 htmx-example/templates/base.html.gwirl create mode 100644 htmx-example/templates/counter.html.gwirl create mode 100644 htmx-example/templates/index.html.gwirl diff --git a/README.md b/README.md index 013153b..d9f1da5 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,10 @@ expression. ## Getting started +> [!NOTE] +> If you are interested in seeing an example of using Gwirl in an [HTMX](https://htmx.org) +> app, take a look [here](/htmx-example) + First, you should install the gwirl tool using `go get`: ``` diff --git a/htmx-example/main.go b/htmx-example/main.go new file mode 100644 index 0000000..3319043 --- /dev/null +++ b/htmx-example/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "log" + "net/http" + + "github.com/gamebox/gwirl/htmx-example/views/html" +) + +type Server struct { + count int +} + + +func (s *Server) renderIndex(rw http.ResponseWriter, req *http.Request) { + rw.Write([]byte(html.Index(s.count))) + rw.WriteHeader(200) +} + +func (s *Server) renderCount(rw http.ResponseWriter, req *http.Request) { + s.count += 1 + rw.Write([]byte(html.Counter(s.count))) + rw.WriteHeader(200) +} + +func main() { + log.Println("Starting up...") + handler := http.NewServeMux() + s := Server{0} + handler.HandleFunc("/", s.renderIndex) + handler.HandleFunc("/count", s.renderCount) + log.Fatalf("%v", http.ListenAndServe(":3000", handler)) +} diff --git a/htmx-example/templates/base.html.gwirl b/htmx-example/templates/base.html.gwirl new file mode 100644 index 0000000..506d9b3 --- /dev/null +++ b/htmx-example/templates/base.html.gwirl @@ -0,0 +1,12 @@ +@(title string, content string) + + + +
+@fmt.Sprintf("%d", count)
diff --git a/htmx-example/templates/index.html.gwirl b/htmx-example/templates/index.html.gwirl new file mode 100644 index 0000000..cd895ce --- /dev/null +++ b/htmx-example/templates/index.html.gwirl @@ -0,0 +1,10 @@ +@(count int) + +@Base("Gwirl HTMX - Home") { +