-
Notifications
You must be signed in to change notification settings - Fork 6
/
if.go
85 lines (74 loc) · 1.29 KB
/
if.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
package htmlgo
import "context"
type IfBuilder struct {
comps []HTMLComponent
set bool
}
func If(v bool, comps ...HTMLComponent) (r *IfBuilder) {
r = &IfBuilder{}
if v {
r.comps = comps
r.set = true
}
return
}
func (b *IfBuilder) ElseIf(v bool, comps ...HTMLComponent) (r *IfBuilder) {
if b.set {
return b
}
if v {
b.comps = comps
b.set = true
}
return b
}
func (b *IfBuilder) Else(comps ...HTMLComponent) (r *IfBuilder) {
if b.set {
return b
}
b.set = true
b.comps = comps
return b
}
func (b *IfBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
if len(b.comps) == 0 {
return
}
return HTMLComponents(b.comps).MarshalHTML(ctx)
}
type IfFuncBuilder struct {
f func() HTMLComponent
set bool
}
func Iff(v bool, f func() HTMLComponent) (r *IfFuncBuilder) {
r = &IfFuncBuilder{}
if v {
r.f = f
r.set = true
}
return
}
func (b *IfFuncBuilder) ElseIf(v bool, f func() HTMLComponent) (r *IfFuncBuilder) {
if b.set {
return b
}
if v {
b.f = f
b.set = true
}
return b
}
func (b *IfFuncBuilder) Else(f func() HTMLComponent) (r *IfFuncBuilder) {
if b.set {
return b
}
b.set = true
b.f = f
return b
}
func (b *IfFuncBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
if b.f == nil {
return
}
return b.f().MarshalHTML(ctx)
}