you-dont-need-react

you-dont-need-react
git clone git://brookjeynes.dev/bjeynes/you-dont-need-react.git
Log | Files | Refs | README

web.go (824B)


      1 package web
      2 
      3 import (
      4 	"embed"
      5 	"encoding/json"
      6 	"html/template"
      7 	"io"
      8 )
      9 
     10 type Task struct {
     11 	ID    string `json:"id"`
     12 	Title string `json:"title"`
     13 }
     14 
     15 type Column struct {
     16 	ID    string
     17 	Title string
     18 	Tasks []Task
     19 }
     20 
     21 //go:embed *.gohtml
     22 var files embed.FS
     23 
     24 var tmpl = template.Must(template.New("").Funcs(template.FuncMap{
     25 	"json": func(v any) (string, error) {
     26 		b, err := json.Marshal(v)
     27 		return string(b), err
     28 	},
     29 }).ParseFS(files, "*.gohtml"))
     30 
     31 func Index(w io.Writer) error {
     32 	return tmpl.ExecuteTemplate(
     33 		w,
     34 		"index",
     35 		[]Column{
     36 			{
     37 				ID:    "todo",
     38 				Title: "To Do",
     39 				Tasks: []Task{
     40 					{ID: "1", Title: "get groceries"},
     41 				},
     42 			},
     43 			{ID: "doing", Title: "Doing"},
     44 			{ID: "done", Title: "Done"},
     45 		},
     46 	)
     47 }
     48 
     49 func TaskRow(w io.Writer, t Task) error {
     50 	return tmpl.ExecuteTemplate(w, "task-row", t)
     51 }