Skip to content

Commit 733326a

Browse files
authored
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.
1 parent 0b6a865 commit 733326a

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ expression.
3333

3434
## Getting started
3535

36+
> [!NOTE]
37+
> If you are interested in seeing an example of using Gwirl in an [HTMX](https://htmx.org)
38+
> app, take a look [here](/htmx-example)
39+
3640
First, you should install the gwirl tool using `go get`:
3741

3842
```

htmx-example/main.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/gamebox/gwirl/htmx-example/views/html"
8+
)
9+
10+
type Server struct {
11+
count int
12+
}
13+
14+
15+
func (s *Server) renderIndex(rw http.ResponseWriter, req *http.Request) {
16+
rw.Write([]byte(html.Index(s.count)))
17+
rw.WriteHeader(200)
18+
}
19+
20+
func (s *Server) renderCount(rw http.ResponseWriter, req *http.Request) {
21+
s.count += 1
22+
rw.Write([]byte(html.Counter(s.count)))
23+
rw.WriteHeader(200)
24+
}
25+
26+
func main() {
27+
log.Println("Starting up...")
28+
handler := http.NewServeMux()
29+
s := Server{0}
30+
handler.HandleFunc("/", s.renderIndex)
31+
handler.HandleFunc("/count", s.renderCount)
32+
log.Fatalf("%v", http.ListenAndServe(":3000", handler))
33+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@(title string, content string)
2+
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<title>@title</title>
7+
<script src="https://unpkg.com/htmx.org@@1.9.10"></script>
8+
</head>
9+
<body>
10+
@content
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@(count int)
2+
3+
@import "fmt"
4+
5+
<p id="counter" class="count">@fmt.Sprintf("%d", count)</p>
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@(count int)
2+
3+
@Base("Gwirl HTMX - Home") {
4+
<h1>Gwirl HTMX</h1>
5+
6+
@Counter(count)
7+
8+
<button hx-patch="/count" hx-target="#counter">Increment</button>
9+
}
10+

0 commit comments

Comments
 (0)