package views import ( "github.com/bouncepaw/mycorrhiza/hyphae/categories" "github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" "html/template" "io" "log" "net/http" "strings" ) const categoriesCardTmpl = `{{$hyphaName := .HyphaName }}` const categoryPageTmpl = `{{$catName := .CatName }}

Category {{$catName}}

{{if len .Hyphae}}

This page lists all hyphae in the category.

{{else}}

This category has no hyphae.

{{end}}
` var ( categoriesCardT *template.Template categoryPageT *template.Template ) func init() { categoriesCardT = template.Must(template. New("category card"). Funcs(template.FuncMap{ "beautifulName": util.BeautifulName, }). Parse(categoriesCardTmpl)) categoryPageT = template.Must(template. New("category page"). Funcs(template.FuncMap{ "beautifulName": util.BeautifulName, }). Parse(categoryPageTmpl)) } func categoryCardHTML(hyphaName string) string { var buf strings.Builder err := categoriesCardT.Execute(&buf, struct { HyphaName string Categories []string }{ hyphaName, categories.WithHypha(hyphaName), }) if err != nil { log.Println(err) } return buf.String() } func CategoryPageHTML(w io.Writer, rq *http.Request, catName string) { var buf strings.Builder err := categoryPageT.Execute(&buf, struct { CatName string Hyphae []string }{ catName, categories.Contents(catName), }) if err != nil { log.Println(err) } io.WriteString(w, BaseHTML( "Category "+util.BeautifulName(catName), buf.String(), l18n.FromRequest(rq), user.FromRequest(rq), )) }