From a656b13ca27e88d213e2b6b98051d67ad5b92837 Mon Sep 17 00:00:00 2001 From: Chrstopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Mon, 5 Aug 2024 00:46:25 -0700 Subject: [PATCH] add some template example and remove Go from readme --- README.md | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index e05388f..b69d327 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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)) @@ -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) @@ -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, @@ -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 } + ``` \ No newline at end of file