Implement yet another template system
This commit is contained in:
parent
326ace8e13
commit
04d59927ab
56
newtmpl/base.html
Normal file
56
newtmpl/base.html
Normal file
@ -0,0 +1,56 @@
|
||||
{{define "confirm"}}Confirm{{end}}
|
||||
{{define "cancel"}}Cancel{{end}}
|
||||
{{define "save"}}Save{{end}}
|
||||
{{define "error"}}Error{{end}}
|
||||
{{define "delete"}}Delete{{end}}
|
||||
{{define "page"}}
|
||||
<!doctype html>
|
||||
<html lang="{{.Meta.Locale}}">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{block "title" .}}{{end}}</title>
|
||||
<link rel="icon" href="/static/favicon.ico">
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
{{range .HeadElements}}{{.}}{{end}}
|
||||
</head>
|
||||
<body data-rrh-addr="{{if .Addr}}{{.Addr}}{{else}}{{.Meta.Addr}}{{end}}"{{range $key, $value := .BodyAttributes}} data-rrh-{{$key}}="{{$value}}"{{end}}>
|
||||
<header>
|
||||
<nav class="main-width top-bar">
|
||||
<ul class="top-bar__wrapper">
|
||||
<li class="top-bar__section top-bar__section_home">
|
||||
<div class="top-bar__home-link-wrapper">
|
||||
<a class="top-bar__home-link" href="/">{{block "wiki name" .}}{{end}}</a>
|
||||
</div>
|
||||
</li>
|
||||
<li class="top-bar__section top-bar__section_search">
|
||||
<form class="top-bar__search" method="GET" action="/title-search">
|
||||
<input type="text" name="q" class="top-bar__search-bar"
|
||||
placeholder="{{block `search by title` .}}Search by title{{end}}">
|
||||
</form>
|
||||
</li>
|
||||
<li class="top-bar__section top-bar__section_auth">
|
||||
{{block "auth" .}}{{end}}
|
||||
</li>
|
||||
<li class="top-bar__section top-bar__section_highlights">
|
||||
<ul class="top-bar__highlights">
|
||||
{{range .HeaderLinks}}
|
||||
<li class="top-bar__highlight">
|
||||
<a class="top-bar__highlight-link" href="{{.Href}}">{{.Display}}</a>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
{{block "body" .}}{{end}}
|
||||
<script src="/static/common.js"></script>
|
||||
<script src="/static/shortcuts.js"></script>
|
||||
<script src="/static/view.js"></script>
|
||||
{{range .CommonScripts}}
|
||||
<script src="{{.}}"></script>
|
||||
{{end}}
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
54
newtmpl/newtmpl.go
Normal file
54
newtmpl/newtmpl.go
Normal file
@ -0,0 +1,54 @@
|
||||
package newtmpl
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"html/template"
|
||||
"strings"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/viewutil"
|
||||
)
|
||||
|
||||
//go:embed *.html
|
||||
var fs embed.FS
|
||||
|
||||
type Page struct {
|
||||
TemplateEnglish *template.Template
|
||||
TemplateRussian *template.Template
|
||||
}
|
||||
|
||||
func NewPage(tmpl string, russianTranslation map[string]string) *Page {
|
||||
must := template.Must
|
||||
|
||||
return &Page{
|
||||
TemplateEnglish: must(template.ParseFS(fs, "base.html")),
|
||||
TemplateRussian: must(must(template.ParseFS(fs, "base.html")).
|
||||
Parse(translationsIntoTemplates(russianTranslation))),
|
||||
}
|
||||
}
|
||||
|
||||
func translationsIntoTemplates(m map[string]string) string {
|
||||
var sb strings.Builder
|
||||
for k, v := range m {
|
||||
sb.WriteString(fmt.Sprintf(`{{define "%s"}}%s{{end}}
|
||||
`, k, v))
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func (p *Page) RenderTo(meta viewutil.Meta, data map[string]any) error {
|
||||
data["Meta"] = meta
|
||||
data["HeadElements"] = meta.HeadElements
|
||||
data["BodyAttributes"] = meta.BodyAttributes
|
||||
data["CommonScripts"] = cfg.CommonScripts
|
||||
data["EditScripts"] = cfg.EditScripts
|
||||
data["HeaderLinks"] = viewutil.HeaderLinks
|
||||
|
||||
tmpl := p.TemplateEnglish
|
||||
if meta.LocaleIsRussian() {
|
||||
tmpl = p.TemplateRussian
|
||||
}
|
||||
|
||||
return tmpl.ExecuteTemplate(meta.W, "page", data)
|
||||
}
|
||||
@ -13,6 +13,10 @@ type Meta struct {
|
||||
U *user.User
|
||||
W io.Writer
|
||||
Addr string
|
||||
|
||||
// New template additions
|
||||
HeadElements []string
|
||||
BodyAttributes map[string]string
|
||||
}
|
||||
|
||||
// MetaFrom makes a Meta from the given data. You are meant to further modify it.
|
||||
@ -28,3 +32,7 @@ func MetaFrom(w http.ResponseWriter, rq *http.Request) Meta {
|
||||
func (m Meta) Locale() string {
|
||||
return m.Lc.Locale
|
||||
}
|
||||
|
||||
func (m Meta) LocaleIsRussian() bool {
|
||||
return m.Locale() == "ru"
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user