-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainers_2nested_test.go
146 lines (123 loc) · 3.21 KB
/
containers_2nested_test.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package containers_test
import (
"fmt"
"net/http"
"strings"
ct "github.com/theplant/containers"
)
func ComplicatedHome(r *http.Request) (cs []ct.Container, err error) {
// Get key parameter in `Page` and pass it to all containers that needed the parameter.
segs := strings.Split(r.RequestURI, "/")
productCode := segs[len(segs)-1]
cs = []ct.Container{
&Header{},
&MainContent{
ProductCode: productCode,
ProductBasicInfo: &ProductBasicInfo{productCode},
ProductImages: &ProductImages{
ProductCode: productCode,
ProductMainImage: ct.ContainerFunc(ProductMainImage),
},
ProductDescription: &ProductDescription{productCode},
},
&Footer{},
}
return
}
/*
### Setup a nested containers tree
Build a tree of containers, by using container struct field and render the children container inside parent container manually.
Note that the struct field name has to be exported, means uppercase. Or it can't benefit `reloading` package.
*/
func ExampleContainer_2nested() {
http.Handle("/page2", ct.PageHandler(ct.PageFunc(ComplicatedHome), nil))
//Output:
}
// Container MainContent
type MainContent struct {
ProductCode string // use struct field to get inputs from outside of a container.
ProductBasicInfo ct.Container // use struct field to pass in a container that used inside another container.
ProductImages ct.Container
ProductDescription ct.Container
}
func (mc *MainContent) Render(r *http.Request) (html string, err error) {
var basicInfoHtml, imagesHtml, descriptionHtml string
// inside the container, call `Render` manually to render inside containers.
if basicInfoHtml, err = mc.ProductBasicInfo.Render(r); err != nil {
return
}
if imagesHtml, err = mc.ProductImages.Render(r); err != nil {
return
}
if descriptionHtml, err = mc.ProductDescription.Render(r); err != nil {
return
}
html = fmt.Sprintf(`
<div class="main-content">
%s
%s
%s
</div>
`,
basicInfoHtml,
imagesHtml,
descriptionHtml,
)
return
}
type ProductBasicInfo struct {
ProductCode string
}
func (bi *ProductBasicInfo) Render(r *http.Request) (html string, err error) {
db := getDb()
p := db.GetProduct(bi.ProductCode)
html = fmt.Sprintf(`
<div class="basic-info">
%s
</div>
`,
p.Name,
)
return
}
type ProductImages struct {
ProductCode string
ProductMainImage ct.Container
}
func (pi *ProductImages) Render(r *http.Request) (html string, err error) {
db := getDb()
var mainImageHtml string
if mainImageHtml, err = pi.Render(r); err != nil {
return
}
images := db.GetProductImages(pi.ProductCode)
html = fmt.Sprintf(`
<div class="images">
%s
%v
</div>
`,
mainImageHtml,
images,
)
return
}
func ProductMainImage(r *http.Request) (html string, err error) {
html = `<div class="product-main-image>main image</div>`
return
}
type ProductDescription struct {
ProductCode string
}
func (pd *ProductDescription) Render(r *http.Request) (html string, err error) {
db := getDb()
desc := db.GetProductDescription(pd.ProductCode)
html = fmt.Sprintf(`
<div class="description">
%s
</div>
`,
desc,
)
return
}