Skip to content

Commit

Permalink
Include *http.Request in HTMLer function
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Dec 8, 2023
1 parent 8aaf3b7 commit f969d80
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions babyapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,10 @@ func (ul *UnorderedList) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

func (ul *UnorderedList) HTML() string {
func (ul *UnorderedList) HTML(r *http.Request) string {
result := "<ul>\n"
for _, li := range ul.Items {
result += li.HTML() + "\n"
result += li.HTML(r) + "\n"
}
return result + "</ul>"
}
Expand All @@ -585,7 +585,7 @@ type ListItem struct {
Content string
}

func (d *ListItem) HTML() string {
func (d *ListItem) HTML(*http.Request) string {
tmpl := template.Must(template.New("li").Parse(`<li>{{ .Content }}</li>`))
return babyapi.MustRenderHTML(tmpl, d)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/todo-htmx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (t *TODO) Patch(newTODO *TODO) *babyapi.ErrResponse {
return nil
}

func (t *TODO) HTML() string {
func (t *TODO) HTML(*http.Request) string {
tmpl := template.Must(template.New("todoRow").Parse(todoRowTemplate))
return babyapi.MustRenderHTML(tmpl, t)
}
Expand All @@ -127,7 +127,7 @@ func (at *AllTODOs) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}

func (at *AllTODOs) HTML() string {
func (at *AllTODOs) HTML(*http.Request) string {
tmpl := template.Must(template.New("todoRow").Parse(todoRowTemplate))
tmpl = template.Must(tmpl.New("allTODOs").Parse(allTODOsTemplate))
return babyapi.MustRenderHTML(tmpl, at)
Expand All @@ -151,7 +151,7 @@ func main() {
api.SetOnCreateOrUpdate(func(r *http.Request, t *TODO) *babyapi.ErrResponse {
if r.Method == http.MethodPost {
select {
case todoChan <- &babyapi.ServerSentEvent{Event: "data", Data: t.HTML()}:
case todoChan <- &babyapi.ServerSentEvent{Event: "data", Data: t.HTML(r)}:
default:
}
}
Expand Down
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// HTMLer allows for easily represending reponses as HTML strings when accepted content
// type is text/html
type HTMLer interface {
HTML() string
HTML(*http.Request) string
}

// Create API routes on the given router
Expand All @@ -21,7 +21,7 @@ func (a *API[T]) Route(r chi.Router) {
if render.GetAcceptedContentType(r) == render.ContentTypeHTML {
htmler, ok := v.(HTMLer)
if ok {
render.HTML(w, r, htmler.HTML())
render.HTML(w, r, htmler.HTML(r))
return
}
}
Expand Down

0 comments on commit f969d80

Please sign in to comment.