mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-28 16:45:32 +00:00
some enhancements
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user