1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/views/categories.go

108 lines
1.9 KiB
Go
Raw Normal View History

package views
import (
"github.com/bouncepaw/mycorrhiza/hyphae/categories"
"github.com/bouncepaw/mycorrhiza/util"
"html/template"
2022-03-20 08:21:59 +00:00
"io"
"log"
"strings"
)
const categoriesRu = `
{{define "empty cat"}}Эта категория пуста.{{end}}
{{define "add hypha"}}Добавить в категорию{{end}}
{{define "cat"}}Категория{{end}}
{{define "hypha name"}}Имя гифы{{end}}
`
2022-03-20 08:21:59 +00:00
var (
categoryT *template.Template
2022-03-20 08:21:59 +00:00
)
func init() {
categoryT = template.Must(template.
New("category").
Funcs(
template.FuncMap{
"beautifulName": util.BeautifulName,
}).
2022-03-22 13:40:40 +00:00
ParseFS(fs, "categories.html"))
}
2022-03-20 21:24:40 +00:00
func categoryCard(hyphaName string) string {
var buf strings.Builder
2022-03-22 13:40:40 +00:00
t, err := categoryT.Clone()
if err != nil {
log.Println(err)
return ""
}
err = t.ExecuteTemplate(&buf, "category card", struct {
HyphaName string
Categories []string
}{
hyphaName,
categories.WithHypha(hyphaName),
})
if err != nil {
log.Println(err)
}
return buf.String()
}
2022-03-20 08:21:59 +00:00
2022-03-20 21:24:40 +00:00
func CategoryPage(meta Meta, catName string) {
2022-03-20 08:21:59 +00:00
var buf strings.Builder
2022-03-22 13:40:40 +00:00
var t, err = categoryT.Clone()
if err != nil {
log.Println(err)
return
}
if meta.Lc.Locale == "ru" {
2022-03-22 13:40:40 +00:00
_, err = t.Parse(categoriesRu)
if err != nil {
log.Println(err)
return
}
}
2022-03-22 13:40:40 +00:00
err = t.ExecuteTemplate(&buf, "category page", struct {
2022-03-20 08:21:59 +00:00
CatName string
Hyphae []string
}{
catName,
categories.Contents(catName),
})
if err != nil {
log.Println(err)
}
2022-03-20 21:24:40 +00:00
_, err = io.WriteString(meta.W, Base(
2022-03-20 08:21:59 +00:00
"Category "+util.BeautifulName(catName),
buf.String(),
meta.Lc,
meta.U,
2022-03-20 08:21:59 +00:00
))
if err != nil {
log.Println(err)
}
2022-03-20 08:21:59 +00:00
}
2022-03-20 21:24:40 +00:00
func CategoryList(meta Meta) {
var buf strings.Builder
err := categoryT.ExecuteTemplate(&buf, "category list", struct {
Categories []string
}{
categories.List(),
})
if err != nil {
log.Println(err)
}
2022-03-20 21:24:40 +00:00
_, err = io.WriteString(meta.W, Base(
"Category list",
buf.String(),
meta.Lc,
meta.U,
))
if err != nil {
log.Println(err)
}
}