some enhancements

This commit is contained in:
Simon Martens
2025-09-16 22:51:53 +02:00
parent 26660f2901
commit 76bd395d23
8 changed files with 353 additions and 332 deletions

View File

@@ -1,6 +1,7 @@
package templating
import (
"fmt"
"html/template"
"io"
"io/fs"
@@ -82,6 +83,22 @@ func (e *Engine) funcs() error {
return result
})
// Template helpers
e.AddFunc("dict", func(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, fmt.Errorf("dict requires an even number of arguments")
}
dict := make(map[string]interface{})
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, fmt.Errorf("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
})
// Strings
e.AddFunc("FirstLetter", functions.FirstLetter)
e.AddFunc("Upper", strings.ToUpper)