Skip to content

Commit

Permalink
update example so it uses form arg
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 30, 2024
1 parent c9f976e commit fc4a1c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
6 changes: 3 additions & 3 deletions example/index.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{{template "index.gohtml" .}}
{{- end -}}

{{- define "GET /fruits/{fruit}/edit GetFormEditRow(fruit)" -}}
{{- define "GET /fruits/{id}/edit GetFormEditRow(id)" -}}
<tr>
<td>{{ .Row.Name }}</td>
<td>
Expand All @@ -49,9 +49,9 @@
</tr>
{{- end -}}

{{- define "PATCH /fruits/{fruit} SubmitFormEditRow(request, fruit)" }}
{{- define "PATCH /fruits/{id} SubmitFormEditRow(id, form)" }}
{{- if .Error -}}
{{template "GET /fruits/{fruit}/edit GetFormEditRow(fruit)" .}}
{{template "GET /fruits/{id}/edit GetFormEditRow(id)" .}}
{{- else -}}
{{template "fruit row" .Row}}
{{- end -}}
Expand Down
13 changes: 6 additions & 7 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"html/template"
"log"
"net/http"
"strconv"
)

//go:embed *.gohtml
Expand All @@ -24,16 +23,16 @@ type EditRowPage struct {
Error error
}

func (b *Backend) SubmitFormEditRow(request *http.Request, fruitID int) EditRowPage {
type EditRow struct {
Value int `input:"count"`
}

func (b *Backend) SubmitFormEditRow(fruitID int, form EditRow) EditRowPage {
if fruitID < 0 || fruitID >= len(b.data) {
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}
count, err := strconv.Atoi(request.FormValue("count"))
if err != nil {
return EditRowPage{Error: err, Row: b.data[fruitID]}
}
row := b.data[fruitID]
row.Value = count
row.Value = form.Value
return EditRowPage{Error: nil, Row: row}
}

Expand Down
30 changes: 19 additions & 11 deletions example/template_routes.go

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

0 comments on commit fc4a1c0

Please sign in to comment.