mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-02-03 20:59:19 +00:00
Categories, views: Refactor the cat page view
This commit is contained in:
parent
c3fafb735f
commit
ca3da43e53
@ -36,31 +36,3 @@
|
||||
</ul>
|
||||
</aside>
|
||||
{{end}}
|
||||
|
||||
{{define "category page"}}
|
||||
{{$catName := .CatName}}
|
||||
<main class="main-width category">
|
||||
<h1>{{block "cat" .}}Category{{end}} <i>{{beautifulName $catName}}</i></h1>
|
||||
{{if len .Hyphae | not}}
|
||||
<p>{{block "empty cat" .}}This category is empty{{end}}</p>
|
||||
{{end}}
|
||||
<ul class="category__entries">
|
||||
{{range .Hyphae}}
|
||||
<li class="category__entry">
|
||||
<a class="category__link" href="/hypha/{{.}}">{{beautifulName .}}</a>
|
||||
</li>
|
||||
{{end}}
|
||||
{{if .GivenPermissionToModify}}
|
||||
<li class="category__entry category__add-to-cat">
|
||||
<form method="POST" action="/add-to-category" class="category__add-form">
|
||||
<input type="text" name="hypha" id="_hypha-name"
|
||||
placeholder="{{block `hypha name` .}}Hypha name{{end}}">
|
||||
<input type="hidden" name="cat" value="{{$catName}}">
|
||||
<input type="hidden" name="redirect-to" value="/category/{{$catName}}">
|
||||
<input type="submit" class="btn" value="{{block `add hypha` .}}Add to the category{{end}}">
|
||||
</form>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</main>
|
||||
{{end}}
|
27
categories/view_page.html
Normal file
27
categories/view_page.html
Normal file
@ -0,0 +1,27 @@
|
||||
{{define "body"}}
|
||||
{{$catName := .CatName}}
|
||||
<main class="main-width category">
|
||||
<h1>{{block "cat" .}}Category{{end}} <i>{{beautifulName $catName}}</i></h1>
|
||||
{{if len .Hyphae | not}}
|
||||
<p>{{block "empty cat" .}}This category is empty{{end}}</p>
|
||||
{{end}}
|
||||
<ul class="category__entries">
|
||||
{{range .Hyphae}}
|
||||
<li class="category__entry">
|
||||
<a class="category__link" href="/hypha/{{.}}">{{beautifulName .}}</a>
|
||||
</li>
|
||||
{{end}}
|
||||
{{if .GivenPermissionToModify}}
|
||||
<li class="category__entry category__add-to-cat">
|
||||
<form method="POST" action="/add-to-category" class="category__add-form">
|
||||
<input type="text" name="hypha" id="_hypha-name"
|
||||
placeholder="{{block `hypha name` .}}Hypha name{{end}}">
|
||||
<input type="hidden" name="cat" value="{{$catName}}">
|
||||
<input type="hidden" name="redirect-to" value="/category/{{$catName}}">
|
||||
<input type="submit" class="btn" value="{{block `add hypha` .}}Add to the category{{end}}">
|
||||
</form>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</main>
|
||||
{{end}}
|
@ -5,7 +5,6 @@ import (
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
"github.com/bouncepaw/mycorrhiza/viewutil"
|
||||
"io"
|
||||
"log"
|
||||
"strings"
|
||||
"text/template" // TODO: Fight
|
||||
@ -29,11 +28,21 @@ var (
|
||||
//go:embed *.html
|
||||
fs embed.FS
|
||||
m = template.Must
|
||||
baseEn, baseRu, listEn, listRu *template.Template
|
||||
baseEn, baseRu, listEn, listRu, pageEn, pageRu *template.Template
|
||||
categoryTemplatesEn *template.Template
|
||||
categoryTemplatesRu *template.Template
|
||||
)
|
||||
|
||||
func loctmp(meta viewutil.Meta, en *template.Template, ru *template.Template) *template.Template {
|
||||
switch meta.Locale() {
|
||||
case "en":
|
||||
return en
|
||||
case "ru":
|
||||
return ru
|
||||
}
|
||||
panic("aaa")
|
||||
}
|
||||
|
||||
func prepareViews() {
|
||||
categoryTemplatesEn = template.Must(template.
|
||||
New("category").
|
||||
@ -45,9 +54,11 @@ func prepareViews() {
|
||||
categoryTemplatesRu = template.Must(template.Must(categoryTemplatesEn.Clone()).Parse(categoriesRu))
|
||||
|
||||
baseEn = m(viewutil.BaseEn.Clone())
|
||||
baseRu = m(viewutil.BaseEn.Clone())
|
||||
baseRu = m(viewutil.BaseRu.Clone())
|
||||
listEn = m(m(baseEn.Clone()).ParseFS(fs, "view_list.html"))
|
||||
listRu = m(m(m(baseRu.Clone()).ParseFS(fs, "view_list.html")).Parse(categoriesRu))
|
||||
pageEn = m(m(baseEn.Clone()).ParseFS(fs, "view_page.html"))
|
||||
pageRu = m(m(m(baseRu.Clone()).ParseFS(fs, "view_page.html")).Parse(categoriesRu))
|
||||
}
|
||||
|
||||
func localizedCatTemplates(meta viewutil.Meta) *template.Template {
|
||||
@ -89,38 +100,27 @@ func CategoryCard(meta viewutil.Meta, hyphaName string) string {
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func categoryPage(meta viewutil.Meta, catName string) {
|
||||
var buf strings.Builder
|
||||
err := localizedCatTemplates(meta).ExecuteTemplate(&buf, "category page", struct {
|
||||
type pageData struct {
|
||||
viewutil.BaseData
|
||||
CatName string
|
||||
Hyphae []string
|
||||
GivenPermissionToModify bool
|
||||
}{
|
||||
catName,
|
||||
Contents(catName),
|
||||
meta.U.CanProceed("add-to-category"),
|
||||
})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
_, err = io.WriteString(meta.W, viewutil.Base(
|
||||
meta,
|
||||
localizedCatTemplateAsString(meta, "category x", catName),
|
||||
buf.String(),
|
||||
))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func loctmp(meta viewutil.Meta, en *template.Template, ru *template.Template) *template.Template {
|
||||
switch meta.Locale() {
|
||||
case "en":
|
||||
return en
|
||||
case "ru":
|
||||
return ru
|
||||
func categoryPage(meta viewutil.Meta, catName string) {
|
||||
if err := loctmp(meta, pageEn, pageRu).ExecuteTemplate(meta.W, "page", pageData{
|
||||
BaseData: viewutil.BaseData{
|
||||
Meta: meta,
|
||||
Title: localizedCatTemplateAsString(meta, "category x", catName),
|
||||
HeaderLinks: cfg.HeaderLinks,
|
||||
CommonScripts: cfg.CommonScripts,
|
||||
},
|
||||
CatName: catName,
|
||||
Hyphae: Contents(catName),
|
||||
GivenPermissionToModify: meta.U.CanProceed("add-to-category"),
|
||||
}); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
panic("aaa")
|
||||
}
|
||||
|
||||
type listData struct {
|
||||
|
Loading…
Reference in New Issue
Block a user