Skip to content

Commit

Permalink
feat: add htmx demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Brereton authored and Tom Brereton committed Jan 10, 2024
1 parent 054528a commit 46ef0bb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/routes/htmx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package routes

import (
"html/template"
"net/http"

"github.com/go-chi/chi/v5"
)

func Htmx(t *template.Template) *chi.Mux {
r := chi.NewRouter()

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
err := t.ExecuteTemplate(w, "htmx-demo.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})

return r
}
1 change: 1 addition & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (s *Server) MountMiddleware() {
func (s *Server) MountPageHandlers() {
s.Router.NotFound(routes.NotFound(s.Templates))
s.Router.Mount("/", routes.LandingPage(s.Templates))
s.Router.Mount("/clicked", routes.Htmx(s.Templates))
}

func (s *Server) MountStaticFiles() {
Expand Down
3 changes: 3 additions & 0 deletions templates/htmx-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ define "htmx-demo.html" }}
<p class="text-slate-500 text-lg">Who needs SPA</p>
{{ end }}
5 changes: 5 additions & 0 deletions templates/landing.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/css/styles.css" rel="stylesheet">
<script src="https://unpkg.com/[email protected]"></script>
</head>

<body>
<h1 id="hello" class="text-3xl font-bold text-blue-500 underline">
Hello, {{ .Name }}!
</h1>
<!-- have a button POST a click via AJAX -->
<button hx-get="/clicked" hx-swap="outerHTML" class="m-2 p-2 rounded-lg bg-slate-800 text-white">
Click Me
</button>
</body>

</html>
Expand Down

0 comments on commit 46ef0bb

Please sign in to comment.