-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
136 lines (122 loc) · 2.28 KB
/
examples_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
package m_test
import (
"fmt"
"os"
"strings"
. "layeh.com/m"
)
func ExampleRender() {
el := M("p",
T("Hello World"),
)
if err := Render(os.Stdout, el); err != nil {
panic(err)
}
// Output:
// <p>Hello World</p>
}
func ExampleRenderString() {
el := M("p",
T("Hello World"),
)
fmt.Println(RenderString(el))
// Output:
// <p>Hello World</p>
}
func ExampleAttr() {
el := M("p", Attr("data-id", "ff34"),
T("User"),
)
fmt.Println(RenderString(el))
// Output:
// <p data-id="ff34">User</p>
}
func ExampleAttrf() {
el := M("div", Attrf("id", "user-%d", 10),
T("User section"),
)
fmt.Println(RenderString(el))
// Output:
// <div id="user-10">User section</div>
}
func ExampleDocument() {
doc := Document(
M("head",
M("title", T("Hello World")),
),
M("body"),
)
if err := Render(os.Stdout, doc); err != nil {
panic(err)
}
// Output:
// <!DOCTYPE html>
// <head><title>Hello World</title></head><body></body>
}
func ExampleF() {
el := M("p",
F("Hello, %s", "World"),
)
fmt.Println(RenderString(el))
// Output:
// <p>Hello, World</p>
}
func ExampleFor() {
el := M("ul",
For(0, 3, 1, func(i int) Element {
return M("li", F("%d", i*10))
}),
)
fmt.Println(RenderString(el))
// Output:
// <ul><li>0</li><li>10</li><li>20</li></ul>
}
func ExampleGroup() {
users := []string{
"Alice",
"Bob",
"Bill",
"Eve",
}
el := Group(len(users), func(i, j int) bool {
// Group users by first letter of name
return users[i][0] == users[j][0]
}, func(i, j int) Element {
return M("p",
T(strings.Join(users[i:j], ", ")),
)
})
fmt.Println(RenderString(el))
// Output:
// <p>Alice</p><p>Bob, Bill</p><p>Eve</p>
}
func ExampleIf() {
el := Range(5, func(i int) Element {
return M("p",
F("%d", i),
If(i%2 == 0, T("!")),
)
})
fmt.Println(RenderString(el))
// Output:
// <p>0!</p><p>1</p><p>2!</p><p>3</p><p>4!</p>
}
func ExampleIfElse() {
el := Range(5, func(i int) Element {
return M("p",
F("%d", i),
IfElse(i%2 == 0, T("!"), T("?")),
)
})
fmt.Println(RenderString(el))
// Output:
// <p>0!</p><p>1?</p><p>2!</p><p>3?</p><p>4!</p>
}
func ExampleM() {
el := M("h1#headline.active.etc[data-id=3]",
T("Hello World"),
)
fmt.Println(RenderString(el))
// Output:
// <h1 id="headline" class="active etc" data-id="3">Hello World</h1>
}