summaryrefslogtreecommitdiff
path: root/internal/router/template.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2025-03-26 16:59:39 +0100
committerLibravatar GitHub <noreply@github.com>2025-03-26 15:59:39 +0000
commitb6e481d63eec15191f2717957682c13ee8a68308 (patch)
tree03cb9fc8bcb5f9eefddee754ad64b9de10c44c39 /internal/router/template.go
parent[chore] bumps our spf13/viper version (#3943) (diff)
downloadgotosocial-b6e481d63eec15191f2717957682c13ee8a68308.tar.xz
[feature] Allow user to choose "gallery" style layout for web view of profile (#3917)
* [feature] Allow user to choose "gallery" style web layout * find a bug and squish it up and all day long you'll have good luck * just a sec * [performance] reindex public timeline + tinker with query a bit * fiddling * should be good now * last bit of finagling, i'm done now i prommy * panic normally
Diffstat (limited to 'internal/router/template.go')
-rw-r--r--internal/router/template.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/internal/router/template.go b/internal/router/template.go
index 70d87add1..51c0c4960 100644
--- a/internal/router/template.go
+++ b/internal/router/template.go
@@ -76,6 +76,10 @@ func LoadTemplates(engine *gin.Engine) error {
// Set additional "include" functions to render
// provided template name using the base template.
+
+ // Include renders the given template with the given data.
+ // Unlike `template`, `include` can be chained with `indent`
+ // to produce nicely-indented HTML.
funcMap["include"] = func(name string, data any) (template.HTML, error) {
var buf strings.Builder
err := tmpl.ExecuteTemplate(&buf, name, data)
@@ -85,6 +89,25 @@ func LoadTemplates(engine *gin.Engine) error {
return noescape(buf.String()), err
}
+ // includeIndex is like `include` but an index can be specified at
+ // `.Index` and data will be nested at `.Item`. Useful when ranging.
+ funcMap["includeIndex"] = func(name string, data any, index int) (template.HTML, error) {
+ var buf strings.Builder
+ withIndex := struct {
+ Item any
+ Index int
+ }{
+ Item: data,
+ Index: index,
+ }
+ err := tmpl.ExecuteTemplate(&buf, name, withIndex)
+
+ // Template was already escaped by
+ // ExecuteTemplate so we can trust it.
+ return noescape(buf.String()), err
+ }
+
+ // includeAttr is like `include` but for element attributes.
funcMap["includeAttr"] = func(name string, data any) (template.HTMLAttr, error) {
var buf strings.Builder
err := tmpl.ExecuteTemplate(&buf, name, data)