-
Notifications
You must be signed in to change notification settings - Fork 91
/
func.go
102 lines (89 loc) · 2.3 KB
/
func.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package handler
import (
"github.com/fuxiaohei/GoBlog/app/model"
"github.com/fuxiaohei/GoInk"
"path"
"strconv"
)
type jsonContext struct {
context *GoInk.Context
data map[string]interface{}
}
// Json creates a json context response.
func Json(context *GoInk.Context, res bool) *jsonContext {
c := new(jsonContext)
c.context = context
c.data = make(map[string]interface{})
c.data["res"] = res
return c
}
func (jc *jsonContext) Set(key string, v interface{}) *jsonContext {
jc.data[key] = v
return jc
}
func (jc *jsonContext) End() {
jc.context.Json(jc.data)
}
type themeContext struct {
context *GoInk.Context
theme string
}
// Theme creates themed context response.
func Theme(context *GoInk.Context) *themeContext {
t := new(themeContext)
t.context = context
t.theme = model.GetSetting("site_theme")
if t.theme == "" {
t.theme = "default"
}
return t
}
func (tc *themeContext) Layout(layout string) *themeContext {
if layout == "" {
tc.context.Layout("")
return tc
}
tc.context.Layout(path.Join(tc.theme, layout))
return tc
}
func (tc *themeContext) Render(tpl string, data map[string]interface{}) {
tc.context.Render(path.Join(tc.theme, tpl), data)
}
func (tc *themeContext) Tpl(tpl string, data map[string]interface{}) string {
return tc.context.Tpl(path.Join(tc.theme, tpl), data)
}
func (tc *themeContext) Has(tpl string) bool {
file := path.Join(tc.theme, tpl)
return tc.context.App().View().Has(file)
}
// CommentHtml returns rendered comment template html with own content.
func CommentHtml(context *GoInk.Context, c *model.Content) string {
thm := Theme(context)
if !thm.Has("comment.html") {
return ""
}
return thm.Tpl("comment", map[string]interface{}{
"Content": c,
"Comments": c.Comments,
})
}
// SidebarHtml returns rendered sidebar template html.
func SidebarHtml(context *GoInk.Context) string {
thm := Theme(context)
if !thm.Has("sidebar.html") {
return ""
}
popSize, _ := strconv.Atoi(model.GetSetting("popular_size"))
if popSize < 1 {
popSize = 4
}
cmtSize, _ := strconv.Atoi(model.GetSetting("recent_comment_size"))
if cmtSize < 1 {
cmtSize = 3
}
return thm.Tpl("sidebar", map[string]interface{}{
"Popular": model.GetPopularArticleList(popSize),
"RecentComment": model.GetCommentRecentList(cmtSize),
"Tags": model.GetContentTags(),
})
}