aboutsummaryrefslogtreecommitdiff
path: root/backend/data.go
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-10-27 16:32:58 +0000
committerAnhgelus Morhtuuzh <william@herges.fr>2025-10-27 16:32:58 +0000
commitdb0b5c34432b4c0135af8c5c885fd6ad348c3691 (patch)
tree8f38b973a8dd3fc22bbd28498004940451621b41 /backend/data.go
parent7bd309f3ca44930c5207b94acc2d425b24d4b369 (diff)
parent1e2ad3a8f8cd2c12786b92210616325a33d1b209 (diff)
Merge pull request '[Feat] Custom sections' (#1) from feat/custom-sections into main
Reviewed-on: https://git.anhgelus.world/anhgelus/small-web/pulls/1
Diffstat (limited to 'backend/data.go')
-rw-r--r--backend/data.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/backend/data.go b/backend/data.go
index 6ca6020..809e6e6 100644
--- a/backend/data.go
+++ b/backend/data.go
@@ -36,6 +36,7 @@ type data struct {
Links []Link
Logo *Logo
Quote string
+ Language string
}
func (d *data) SetData(data *data) {
@@ -74,6 +75,9 @@ func (d *data) merge(cfg *Config, r *http.Request) {
}
d.URL = r.URL.Path
}
+ if d.Language == "" {
+ d.Language = cfg.Language
+ }
}
func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string, custom dataUsable) {
@@ -92,6 +96,7 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string
},
"next": func(i int) int { return i + 1 },
"before": func(i int) int { return i - 1 },
+ "first": templateFirst,
}).ParseFS(templates, "templates/components.html", fmt.Sprintf("templates/%s.html", name), "templates/base.html")
if err != nil {
panic(err)
@@ -116,7 +121,15 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string
func (d *data) handleRSS(w http.ResponseWriter, r *http.Request, custom dataUsable) {
cfg := r.Context().Value(configKey).(*Config)
d.merge(cfg, r)
- t, err := txt.ParseFS(templates, "templates/rss.xml")
+ t, err := txt.New("").Funcs(txt.FuncMap{
+ "first": templateFirst,
+ "uri": func(s string) string {
+ if s == "" {
+ return ""
+ }
+ return s + "/"
+ },
+ }).ParseFS(templates, "templates/rss.xml")
if err != nil {
panic(err)
}
@@ -197,3 +210,10 @@ func getAsset(ctx context.Context, path string) *assetData {
assets[path] = asset
return asset
}
+
+func templateFirst(a []*Section) *Section {
+ if len(a) == 0 {
+ return nil
+ }
+ return a[0]
+}