Skip to content

Commit

Permalink
focus on the templates
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 20, 2024
1 parent a57f552 commit cae1039
Showing 1 changed file with 77 additions and 74 deletions.
151 changes: 77 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,6 @@ This is especially helpful when you are writing HTMX.

Given [main.go](#main_go) and [index.gohtml](#index_gohtml), muxt will generate [template_routes.go](#template_routes_go).

### <span id='main_go'></span> main.go
```go
package main

import (
"context"
"embed"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
)

//go:embed *.gohtml
var templateSource embed.FS

var templates = template.Must(template.ParseFS(templateSource, "*"))

type Backend struct {
data []Row
}

type EditRowPage struct {
Row Row
Error error
}

func (b *Backend) SubmitFormEditRow(request *http.Request, fruit string) EditRowPage {
count, err := strconv.Atoi(request.FormValue("count"))
if err != nil {
return EditRowPage{Error: err, Row: Row{Name: fruit}}
}
for i := range b.data {
if b.data[i].Name == fruit {
b.data[i].Value = count
return EditRowPage{Error: nil, Row: b.data[i]}
}
}
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}

func (b *Backend) GetFormEditRow(fruit string) EditRowPage {
for i := range b.data {
if b.data[i].Name == fruit {
return EditRowPage{Error: nil, Row: b.data[i]}
}
}
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}

type Row struct {
Name string
Value int
}

func (b *Backend) List(_ context.Context) []Row { return b.data }

//go:generate muxt generate --receiver Backend

func main() {
backend := &Backend{
data: []Row{
{Name: "Peach", Value: 10},
{Name: "Plum", Value: 20},
{Name: "Pineapple", Value: 2},
},
}
mux := http.NewServeMux()
Routes(mux, backend)
log.Fatal(http.ListenAndServe(":8080", mux))
}
```

### <span id='index_gohtml'></span> index.gohtml
```html
<!DOCTYPE html>
Expand Down Expand Up @@ -158,6 +84,83 @@ func main() {
{{end}}
```

### <span id='main_go'></span> main.go

In your Go code focus on your domain. Let muxt generate handlers and wire them up with the HTTP mux (multiplexer).

```go
package main

import (
"context"
"embed"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
)

//go:embed *.gohtml
var templateSource embed.FS

var templates = template.Must(template.ParseFS(templateSource, "*"))

type Backend struct {
data []Row
}

type EditRowPage struct {
Row Row
Error error
}

func (b *Backend) SubmitFormEditRow(request *http.Request, fruit string) EditRowPage {
count, err := strconv.Atoi(request.FormValue("count"))
if err != nil {
return EditRowPage{Error: err, Row: Row{Name: fruit}}
}
for i := range b.data {
if b.data[i].Name == fruit {
b.data[i].Value = count
return EditRowPage{Error: nil, Row: b.data[i]}
}
}
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}

func (b *Backend) GetFormEditRow(fruit string) EditRowPage {
for i := range b.data {
if b.data[i].Name == fruit {
return EditRowPage{Error: nil, Row: b.data[i]}
}
}
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}

type Row struct {
Name string
Value int
}

func (b *Backend) List(_ context.Context) []Row { return b.data }

//go:generate muxt generate --receiver Backend

func main() {
backend := &Backend{
data: []Row{
{Name: "Peach", Value: 10},
{Name: "Plum", Value: 20},
{Name: "Pineapple", Value: 2},
},
}
mux := http.NewServeMux()
Routes(mux, backend)
log.Fatal(http.ListenAndServe(":8080", mux))
}
```

### <span id='template_routes_go'></span> template_routes.go
This file is generated by running `go generate` in the same directory as `main.go`
```go
Expand Down

0 comments on commit cae1039

Please sign in to comment.