forked from ungerik/go-start
/
if.go
53 lines (47 loc) · 1.02 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
package view
///////////////////////////////////////////////////////////////////////////////
// If
type If struct {
ViewBaseWithId
Condition bool
Content View
ElseContent View
}
func (self *If) Init(thisView View) {
if thisView == self.thisView {
return // already initialized
}
self.ViewBaseWithId.Init(thisView)
// ViewBaseWithId.Init() initializes the child reported by IterateChildren(),
// we need to initialize the child for the other case of !self.Condition
var child View
if !self.Condition {
child = self.Content
} else {
child = self.ElseContent
}
if child != nil {
child.Init(child)
}
}
func (self *If) IterateChildren(callback IterateChildrenCallback) {
var child View
if self.Condition {
child = self.Content
} else {
child = self.ElseContent
}
if child != nil {
callback(self, child)
}
}
func (self *If) Render(ctx *Context) (err error) {
content := self.Content
if !self.Condition {
content = self.ElseContent
}
if content == nil {
return nil
}
return content.Render(ctx)
}