Skip to content

Commit

Permalink
feat: add htmx
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Brereton authored and Tom Brereton committed Jan 12, 2024
1 parent a7dd98e commit 4d4e52e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.21.5

require (
github.com/a-h/templ v0.2.513
github.com/go-chi/chi v1.5.5
github.com/go-chi/chi/v5 v5.0.11
github.com/mavolin/go-htmx v1.0.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/a-h/templ v0.2.513 h1:ZmwGAOx4NYllnHy+FTpusc4+c5msoMpPIYX0Oy3dNqw=
github.com/a-h/templ v0.2.513/go.mod h1:9gZxTLtRzM3gQxO8jr09Na0v8/jfliS97S9W5SScanM=
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mavolin/go-htmx v1.0.0 h1:43rZuemWd23zrMcTU939EsflXjOPxtHy9VraT1CZ6qQ=
github.com/mavolin/go-htmx v1.0.0/go.mod h1:r6O09gzKou9kutq3UiDPZ//Q7IeBCMcs8US5/sHFbvg=
20 changes: 16 additions & 4 deletions handler/marketing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package handler
import (
"net/http"

"github.com/mavolin/go-htmx"

"github.com/tombrereton/go-hot-reload/model"
"github.com/tombrereton/go-hot-reload/view/layout"
"github.com/tombrereton/go-hot-reload/view/marketing"
Expand All @@ -14,12 +16,22 @@ type Marketing struct {
func (h *Marketing) GetLandingPage(w http.ResponseWriter, r *http.Request) {
user := model.User{ID: 1, Name: "Bob Loblaw"}
p := marketing.LandingPage(user)
b := layout.Base(p)
b.Render(r.Context(), w)

if htmx.Request(r) != nil {
p.Render(r.Context(), w)
} else {
b := layout.Base(p)
b.Render(r.Context(), w)
}
}

func (h *Marketing) GetAboutPage(w http.ResponseWriter, r *http.Request) {
p := marketing.AboutPage()
b := layout.Base(p)
b.Render(r.Context(), w)

if htmx.Request(r) != nil {
p.Render(r.Context(), w)
} else {
b := layout.Base(p)
b.Render(r.Context(), w)
}
}
14 changes: 13 additions & 1 deletion view/components/header.templ
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package components

templ Header() {
<h2>Header</h2>
<h2>Header</h2>
<button
hx-get="/"
hx-target="#main"
hx-swap="innerHTML"
type="button"
>Home</button>
<button
hx-get="/about"
hx-target="#main"
hx-swap="innerHTML"
type="button"
>About</button>
}
20 changes: 19 additions & 1 deletion view/components/header_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions view/layout/base.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package layout

import "github.com/tombrereton/go-hot-reload/view/components"


templ Base(contents templ.Component) {
<html>
<head>
<title>My Page</title>
<script src="https://unpkg.com/[email protected]"></script>
</head>
<body>
@components.Header()
@components.Header()
<h2>Layout</h2>
@contents
@components.Footer()
<main id="main">
@contents
</main>
@components.Footer()
</body>
</html>
}
21 changes: 17 additions & 4 deletions view/layout/base_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d4e52e

Please sign in to comment.