mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-02-09 15:40:12 +00:00
Categories, views: Refactor the cat page view
This commit is contained in:
parent
c3fafb735f
commit
ca3da43e53
@ -36,31 +36,3 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</aside>
|
</aside>
|
||||||
{{end}}
|
{{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/cfg"
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"github.com/bouncepaw/mycorrhiza/util"
|
||||||
"github.com/bouncepaw/mycorrhiza/viewutil"
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template" // TODO: Fight
|
"text/template" // TODO: Fight
|
||||||
@ -29,11 +28,21 @@ var (
|
|||||||
//go:embed *.html
|
//go:embed *.html
|
||||||
fs embed.FS
|
fs embed.FS
|
||||||
m = template.Must
|
m = template.Must
|
||||||
baseEn, baseRu, listEn, listRu *template.Template
|
baseEn, baseRu, listEn, listRu, pageEn, pageRu *template.Template
|
||||||
categoryTemplatesEn *template.Template
|
categoryTemplatesEn *template.Template
|
||||||
categoryTemplatesRu *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() {
|
func prepareViews() {
|
||||||
categoryTemplatesEn = template.Must(template.
|
categoryTemplatesEn = template.Must(template.
|
||||||
New("category").
|
New("category").
|
||||||
@ -45,9 +54,11 @@ func prepareViews() {
|
|||||||
categoryTemplatesRu = template.Must(template.Must(categoryTemplatesEn.Clone()).Parse(categoriesRu))
|
categoryTemplatesRu = template.Must(template.Must(categoryTemplatesEn.Clone()).Parse(categoriesRu))
|
||||||
|
|
||||||
baseEn = m(viewutil.BaseEn.Clone())
|
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"))
|
listEn = m(m(baseEn.Clone()).ParseFS(fs, "view_list.html"))
|
||||||
listRu = m(m(m(baseRu.Clone()).ParseFS(fs, "view_list.html")).Parse(categoriesRu))
|
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 {
|
func localizedCatTemplates(meta viewutil.Meta) *template.Template {
|
||||||
@ -89,38 +100,27 @@ func CategoryCard(meta viewutil.Meta, hyphaName string) string {
|
|||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func categoryPage(meta viewutil.Meta, catName string) {
|
type pageData struct {
|
||||||
var buf strings.Builder
|
viewutil.BaseData
|
||||||
err := localizedCatTemplates(meta).ExecuteTemplate(&buf, "category page", struct {
|
|
||||||
CatName string
|
CatName string
|
||||||
Hyphae []string
|
Hyphae []string
|
||||||
GivenPermissionToModify bool
|
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 {
|
func categoryPage(meta viewutil.Meta, catName string) {
|
||||||
switch meta.Locale() {
|
if err := loctmp(meta, pageEn, pageRu).ExecuteTemplate(meta.W, "page", pageData{
|
||||||
case "en":
|
BaseData: viewutil.BaseData{
|
||||||
return en
|
Meta: meta,
|
||||||
case "ru":
|
Title: localizedCatTemplateAsString(meta, "category x", catName),
|
||||||
return ru
|
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 {
|
type listData struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user