-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
93 lines (81 loc) · 2 KB
/
options.go
File metadata and controls
93 lines (81 loc) · 2 KB
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
package template
import "net/http"
// Options 可选参数列表
type Options struct {
StatusCode int
Layout string
GlobalVariable map[string]any
GlobalConstant map[string]any
Suffix string
// 主题相关字段
Theme string // 指定的主题名称
DefaultTheme string // 默认主题名称
MultiThemeMode bool // 是否启用多主题模式
}
// newOptions 创建可选参数
func newOptions(opts ...Option) Options {
opt := Options{
StatusCode: http.StatusOK,
Layout: "layout.tmpl",
GlobalVariable: map[string]any{},
GlobalConstant: map[string]any{},
Suffix: "tmpl",
// 主题相关默认值
Theme: "", // 空字符串表示未指定主题
DefaultTheme: "", // 空字符串表示使用自动检测的默认主题
MultiThemeMode: false, // 默认关闭多主题模式,使用自动检测
}
for _, o := range opts {
o(&opt)
}
return opt
}
// Option 为可选参数赋值的函数
type Option func(*Options)
// StatusCode ...
func StatusCode(statusCode int) Option {
return func(o *Options) {
o.StatusCode = statusCode
}
}
// Layout ...
func Layout(layout string) Option {
return func(o *Options) {
o.Layout = layout
}
}
// GlobalVariable ...
func GlobalVariable(variable map[string]any) Option {
return func(o *Options) {
o.GlobalVariable = variable
}
}
// GlobalConstant ...
func GlobalConstant(constant map[string]any) Option {
return func(o *Options) {
o.GlobalConstant = constant
}
}
func Suffix(suffix string) Option {
return func(o *Options) {
o.Suffix = suffix
}
}
// SetTheme 设置指定的主题名称
func SetTheme(themeName string) Option {
return func(o *Options) {
o.Theme = themeName
}
}
// DefaultTheme 设置默认主题名称
func DefaultTheme(themeName string) Option {
return func(o *Options) {
o.DefaultTheme = themeName
}
}
// EnableMultiTheme 启用或禁用多主题模式
func EnableMultiTheme(enable bool) Option {
return func(o *Options) {
o.MultiThemeMode = enable
}
}