Skip to content

Commit

Permalink
add some template example and remove Go from readme
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 5, 2024
1 parent 5a0f01c commit a656b13
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This is especially helpful when you are writing HTMX.

## Example

The "define" blocks in the following template register handlers with the server mux.
The http method, http host, and path semantics match those of in the HTTP package.
This library extends this to add custom data handler invocations see "PATCH /fruits/{fruit}". It is configured to call EditRow on template parse time provided receiver.

```html
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -100,18 +104,26 @@ import (
var formHTML embed.FS

func main() {
s := &Server{
templates: template.Must(template.ParseFS(formHTML, "*")),
table: []Row{
s := &Handlers{
data: []Row{
{Fruit: "Peach", Count: 1},
{Fruit: "Pear", Count: 2},
{Fruit: "Plum", Count: 3},
{Fruit: "Pineapple", Count: 4},
},
}
templates := template.Must(template.ParseFS(formHTML, "*"))
mux := http.NewServeMux()
mux.HandleFunc("/", s.Index)
if err := muxt.Handlers(mux, s.templates, muxt.WithReceiver(s).WithErrorFunc(noopErr)); err != nil {
mux.HandleFunc("/{$}", func(res http.ResponseWriter, req *http.Request) {
buf := bytes.NewBuffer(nil)
if err := templates.ExecuteTemplate(buf, "form.gohtml", s.data); err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
_, _ = io.Copy(res, buf)
})
if err := muxt.Handlers(mux, templates, muxt.WithReceiver(s).WithErrorFunc(noopErr)); err != nil {
log.Fatal(err)
}
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), "8080"), mux))
Expand All @@ -124,22 +136,11 @@ type Row struct {
Count int
}

type Server struct {
table []Row
templates *template.Template
type Handlers struct {
data []Row
}

func (s *Server) Index(res http.ResponseWriter, _ *http.Request) {
buf := bytes.NewBuffer(nil)
if err := s.templates.ExecuteTemplate(buf, "form.gohtml", s.table); err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
_, _ = io.Copy(res, buf)
}

func (s *Server) EditRow(res http.ResponseWriter, req *http.Request, fruit string) (Row, error) {
func (s *Handlers) EditRow(res http.ResponseWriter, req *http.Request, fruit string) (Row, error) {
count, err := strconv.Atoi(req.FormValue("count"))
if err != nil {
http.Error(res, "failed to parse count: "+err.Error(), http.StatusBadRequest)
Expand All @@ -150,13 +151,13 @@ func (s *Server) EditRow(res http.ResponseWriter, req *http.Request, fruit strin
http.Error(res, err.Error(), http.StatusBadRequest)
return Row{}, err
}
for i, row := range s.table {
for i, row := range s.data {
if row.Fruit != fruit {
continue
}
res.Header().Set("HX-Retarget", "closest tr")
res.Header().Set("HX-Reswap", "outerHTML")
s.table[i].Count = count
s.data[i].Count = count
res.WriteHeader(http.StatusOK)
return Row{
Fruit: fruit,
Expand All @@ -167,4 +168,5 @@ func (s *Server) EditRow(res http.ResponseWriter, req *http.Request, fruit strin
http.Error(res, err.Error(), http.StatusNotFound)
return Row{}, err
}

```

0 comments on commit a656b13

Please sign in to comment.