diff options
Diffstat (limited to 'modules/templates/helper.go')
-rw-r--r-- | modules/templates/helper.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 8e1a79bc57..c3d9e8fec2 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -189,6 +189,32 @@ func NewFuncMap() []template.FuncMap { "DefaultTheme": func() string { return setting.UI.DefaultTheme }, + "dict": func(values ...interface{}) (map[string]interface{}, error) { + if len(values) == 0 { + return nil, errors.New("invalid dict call") + } + + dict := make(map[string]interface{}) + + for i := 0; i < len(values); i++ { + switch key := values[i].(type) { + case string: + i++ + if i == len(values) { + return nil, errors.New("specify the key for non array values") + } + dict[key] = values[i] + case map[string]interface{}: + m := values[i].(map[string]interface{}) + for i, v := range m { + dict[i] = v + } + default: + return nil, errors.New("dict values must be maps") + } + } + return dict, nil + }, }} } |