/
plugin.go
76 lines (67 loc) · 1.57 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package nut
import (
"errors"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
const (
// TITLE page title's key
TITLE = "title"
)
// Plugin controller
type Plugin struct {
Controller
}
func init() {
beego.AddFuncMap("links", func(loc string) ([]Link, error) {
var items []Link
_, err := orm.NewOrm().QueryTable(new(Link)).
Filter("loc", loc).
OrderBy("sort_order").
All(&items, "sort_order", "href", "label")
return items, err
})
beego.AddFuncMap("cards", func(loc string) ([]Card, error) {
var items []Card
_, err := orm.NewOrm().QueryTable(new(Card)).
Filter("loc", loc).
OrderBy("sort_order").
All(&items, "title", "href", "logo", "summary", "type", "action")
return items, err
})
beego.AddFuncMap("dtf", func(t time.Time) string {
return t.Format(time.RFC822)
})
beego.AddFuncMap("df", func(t time.Time) string {
return t.Format(DateFormat)
})
beego.AddFuncMap("split", strings.Split)
beego.AddFuncMap(
"dict",
func(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
},
)
RegisterSitemap(func() ([]string, error) {
return []string{
"/users/sign-in",
"/users/sign-up",
"/users/forgot-password",
"/users/confirm",
"/users/unlock",
}, nil
})
}