Skip to content

Commit

Permalink
Merge pull request #59 from calvinmclean/fix/examples
Browse files Browse the repository at this point in the history
Improve HTMX examples
  • Loading branch information
calvinmclean committed Apr 23, 2024
2 parents 2731a39 + 29e7acd commit c4d3d70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 2 additions & 0 deletions examples/event-rsvp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ func createAPI() *API {
),
}

api.Events.AddCustomRootRoute(http.MethodGet, "/", http.RedirectHandler("/events", http.StatusFound))

api.Invites.ApplyExtension(extensions.HTMX[*Invite]{})

api.Invites.AddCustomRoute(http.MethodPost, "/bulk", api.Events.GetRequestedResourceAndDo(api.addBulkInvites))
Expand Down
19 changes: 9 additions & 10 deletions examples/todo-htmx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
allTODOs html.Template = "allTODOs"
allTODOsTemplate = `<!doctype html>
allTODOsTemplate string = `<!doctype html>
<html>
<head>
<meta charset="UTF-8">
Expand Down Expand Up @@ -69,7 +69,7 @@ const (
</html>`

todoRow html.Template = "todoRow"
todoRowTemplate = `<tr hx-target="this" hx-swap="outerHTML">
todoRowTemplate string = `<tr hx-target="this" hx-swap="outerHTML">
<td>{{ .Title }}</td>
<td>{{ .Description }}</td>
<td>
Expand Down Expand Up @@ -109,30 +109,29 @@ type TODO struct {
}

func (t *TODO) HTML(r *http.Request) string {
// tmpl := template.Must(template.New("todoRow").Parse(todoRowTemplate))
// return babyapi.MustRenderHTML(tmpl, t)
return todoRow.Render(r, t)
}

type AllTODOs []*TODO
type AllTODOs struct {
babyapi.ResourceList[*TODO]
}

func (at AllTODOs) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

func (at AllTODOs) HTML(r *http.Request) string {
// tmpl := template.Must(template.New("todoRow").Parse(todoRowTemplate))
// tmpl = template.Must(tmpl.New("allTODOs").Parse(allTODOsTemplate))
// return babyapi.MustRenderHTML(tmpl, at)
return allTODOs.Render(r, at)
return allTODOs.Render(r, at.Items)
}

func createAPI() *babyapi.API[*TODO] {
api := babyapi.NewAPI("TODOs", "/todos", func() *TODO { return &TODO{} })

api.AddCustomRootRoute(http.MethodGet, "/", http.RedirectHandler("/todos", http.StatusFound))

// Use AllTODOs in the GetAll response since it implements HTMLer
api.SetGetAllResponseWrapper(func(todos []*TODO) render.Renderer {
return AllTODOs(todos)
return AllTODOs{ResourceList: babyapi.ResourceList[*TODO]{todos}}
})

api.ApplyExtension(extensions.HTMX[*TODO]{})
Expand Down

0 comments on commit c4d3d70

Please sign in to comment.