mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-13 14:00:25 +00:00
ea0c2f35d1
* Introduce views.Meta for passing common stuff around. * Store both category-related templates in one HTML file, which is go:embed:ded. * Fix a bug.
67 lines
1.1 KiB
Go
67 lines
1.1 KiB
Go
package views
|
|
|
|
import (
|
|
"embed"
|
|
"github.com/bouncepaw/mycorrhiza/hyphae/categories"
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
"html/template"
|
|
"io"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed categories.html
|
|
var fs embed.FS
|
|
|
|
var (
|
|
categoryT *template.Template
|
|
)
|
|
|
|
func init() {
|
|
categoryT = template.Must(template.
|
|
New("category").
|
|
Funcs(
|
|
template.FuncMap{
|
|
"beautifulName": util.BeautifulName,
|
|
}).
|
|
ParseFS(fs, "*"))
|
|
}
|
|
|
|
func categoryCardHTML(hyphaName string) string {
|
|
var buf strings.Builder
|
|
err := categoryT.ExecuteTemplate(&buf, "category card", struct {
|
|
HyphaName string
|
|
Categories []string
|
|
}{
|
|
hyphaName,
|
|
categories.WithHypha(hyphaName),
|
|
})
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func CategoryPageHTML(meta Meta, catName string) {
|
|
var buf strings.Builder
|
|
err := categoryT.ExecuteTemplate(&buf, "category page", struct {
|
|
CatName string
|
|
Hyphae []string
|
|
}{
|
|
catName,
|
|
categories.Contents(catName),
|
|
})
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
_, err = io.WriteString(meta.W, BaseHTML(
|
|
"Category "+util.BeautifulName(catName),
|
|
buf.String(),
|
|
meta.Lc,
|
|
meta.U,
|
|
))
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|