Skip to content

Commit

Permalink
example: add bare-bones HTMX example (#15)
Browse files Browse the repository at this point in the history
Closes #8

Obviously it's the simplest, silliest example possible,
but we can build upon it as we go along.
  • Loading branch information
gamebox authored Jan 7, 2024
1 parent 0b6a865 commit 733326a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

```
Expand Down
33 changes: 33 additions & 0 deletions htmx-example/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
12 changes: 12 additions & 0 deletions htmx-example/templates/base.html.gwirl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@(title string, content string)

<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<script src="https://unpkg.com/htmx.org@@1.9.10"></script>
</head>
<body>
@content
</body>
</html>
5 changes: 5 additions & 0 deletions htmx-example/templates/counter.html.gwirl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@(count int)

@import "fmt"

<p id="counter" class="count">@fmt.Sprintf("%d", count)</p>
10 changes: 10 additions & 0 deletions htmx-example/templates/index.html.gwirl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@(count int)

@Base("Gwirl HTMX - Home") {
<h1>Gwirl HTMX</h1>

@Counter(count)

<button hx-patch="/count" hx-target="#counter">Increment</button>
}

0 comments on commit 733326a

Please sign in to comment.