forked from tyler-sommer/stick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
270 lines (238 loc) · 6.45 KB
/
example_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package stick_test
import (
"fmt"
"os"
"strconv"
"bytes"
"io/ioutil"
"github.com/tyler-sommer/stick"
)
// An example of executing a template in the simplest possible manner.
func ExampleEnv_Execute() {
env := stick.NewEnv(nil)
params := map[string]stick.Value{"name": "World"}
err := env.Execute(`Hello, {{ name }}!`, os.Stdout, params)
if err != nil {
fmt.Println(err)
}
// Output: Hello, World!
}
type exampleType struct{}
func (e exampleType) Boolean() bool {
return true
}
func (e exampleType) Number() float64 {
return 3.14
}
func (e exampleType) String() string {
return "some kinda string"
}
// This demonstrates how a type can be coerced to a boolean.
// The struct in this example has the Boolean method implemented.
//
// func (e exampleType) Boolean() bool {
// return true
// }
func ExampleBoolean() {
v := exampleType{}
fmt.Printf("%t", stick.CoerceBool(v))
// Output: true
}
// This example demonstrates how various values are coerced to boolean.
func ExampleCoerceBool() {
v0 := ""
v1 := "some string"
v2 := 0
v3 := 3.14
fmt.Printf("%t %t %t %t", stick.CoerceBool(v0), stick.CoerceBool(v1), stick.CoerceBool(v2), stick.CoerceBool(v3))
// Output: false true false true
}
// This demonstrates how a type can be coerced to a number.
// The struct in this example has the Number method implemented.
//
// func (e exampleType) Number() float64 {
// return 3.14
// }
func ExampleNumber() {
v := exampleType{}
fmt.Printf("%.2f", stick.CoerceNumber(v))
// Output: 3.14
}
// This example demonstrates how various values are coerced to number.
func ExampleCoerceNumber() {
v0 := true
v1 := ""
v2 := "54"
v3 := "1.33"
fmt.Printf("%.f %.f %.f %.2f", stick.CoerceNumber(v0), stick.CoerceNumber(v1), stick.CoerceNumber(v2), stick.CoerceNumber(v3))
// Output: 1 0 54 1.33
}
// This example demonstrates how a type can be coerced to a string.
// The struct in this example has the String method implemented.
//
// func (e exampleType) String() string {
// return "some kinda string"
// }
func ExampleStringer() {
v := exampleType{}
fmt.Printf("%s", v)
// Output: some kinda string
}
// This demonstrates how various values are coerced to string.
func ExampleCoerceString() {
v0 := true
v1 := false // Coerces into ""
v2 := 54
v3 := 1.33
v4 := 0
fmt.Printf("%s '%s' %s %s %s", stick.CoerceString(v0), stick.CoerceString(v1), stick.CoerceString(v2), stick.CoerceString(v3), stick.CoerceString(v4))
// Output: 1 '' 54 1.33 0
}
// A simple test to check if a value is empty
func ExampleTest() {
env := stick.NewEnv(nil)
env.Tests["empty"] = func(ctx stick.Context, val stick.Value, args ...stick.Value) bool {
return stick.CoerceBool(val) == false
}
err := env.Execute(
`{{ (false is empty) ? 'empty' : 'not empty' }} - {{ ("a string" is empty) ? 'empty' : 'not empty' }}`,
os.Stdout,
nil,
)
if err != nil {
fmt.Println(err)
}
// Output: empty - not empty
}
// A test made up of two words that takes an argument.
func ExampleTest_twoWordsWithArgs() {
env := stick.NewEnv(nil)
env.Tests["divisible by"] = func(ctx stick.Context, val stick.Value, args ...stick.Value) bool {
if len(args) != 1 {
return false
}
i := stick.CoerceNumber(args[0])
if i == 0 {
return false
}
v := stick.CoerceNumber(val)
return int(v)%int(i) == 0
}
err := env.Execute(
`{{ ('something' is divisible by(3)) ? "yep, 'something' evals to 0" : 'nope' }} - {{ (9 is divisible by(3)) ? 'sure' : 'nope' }} - {{ (4 is divisible by(3)) ? 'sure' : 'nope' }}`,
os.Stdout,
nil,
)
if err != nil {
fmt.Println(err)
}
// Output: yep, 'something' evals to 0 - sure - nope
}
// A contrived example of a user-defined function.
func ExampleFunc() {
env := stick.NewEnv(nil)
env.Functions["get_post"] = func(ctx stick.Context, args ...stick.Value) stick.Value {
if len(args) == 0 {
return nil
}
return struct {
Title string
ID float64
}{"A post", stick.CoerceNumber(args[0])}
}
err := env.Execute(
`{% set post = get_post(123) %}{{ post.Title }} (# {{ post.ID }})`,
os.Stdout,
nil,
)
if err != nil {
fmt.Println(err)
}
// Output: A post (# 123)
}
// A simple user-defined filter.
func ExampleFilter() {
env := stick.NewEnv(nil)
env.Filters["raw"] = func(ctx stick.Context, val stick.Value, args ...stick.Value) stick.Value {
return stick.NewSafeValue(val)
}
err := env.Execute(
`{{ name|raw }}`,
os.Stdout,
map[string]stick.Value{"name": "<name>"},
)
if err != nil {
fmt.Println(err)
}
// Output: <name>
}
// A simple user-defined filter that accepts a parameter.
func ExampleFilter_withParam() {
env := stick.NewEnv(nil)
env.Filters["number_format"] = func(ctx stick.Context, val stick.Value, args ...stick.Value) stick.Value {
var d float64
if len(args) > 0 {
d = stick.CoerceNumber(args[0])
}
return strconv.FormatFloat(stick.CoerceNumber(val), 'f', int(d), 64)
}
err := env.Execute(
`${{ price|number_format(2) }}`,
os.Stdout,
map[string]stick.Value{"price": 4.99},
)
if err != nil {
fmt.Println(err)
}
// Output: $4.99
}
func ExampleFunc_usingContext() {
env := stick.NewEnv(&stick.MemoryLoader{
Templates: map[string]string{
"base.html.twig": `<!doctype html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block nav %}{% endblock %}
#3 base: {{ current_template() }}
{% block content %}{% endblock %}
</body>
</html>
`,
"side.html.twig": `{% block nav %}#2 side: {{ current_template() }}{% endblock %}`,
"child.html.twig": `{% extends 'base.html.twig' %}
{% use 'side.html.twig' %}
{% block title %}#1 child: {{ current_template() }}{% endblock %}
{% block content %}#4 child: {{ current_template() }}{% endblock %}`,
},
})
buf := &bytes.Buffer{}
env.Functions["current_template"] = func(ctx stick.Context, args ...stick.Value) stick.Value {
// Reading persistent metadata
v, _ := ctx.Meta().Get("current_template_calls")
nc := stick.CoerceNumber(v)
nc++
fmt.Fprintf(buf, "#%.0f Current Template: %s\n", nc, ctx.Name())
// Writing persistent metadata
ctx.Meta().Set("current_template_calls", stick.CoerceString(nc))
return nil
}
// Notice that we discard the actual output. We only care about what the
// current_template function writes to buf.
err := env.Execute(
`child.html.twig`,
ioutil.Discard,
nil,
)
if err != nil {
fmt.Println(err)
}
fmt.Println(buf.String())
// Output:
// #1 Current Template: child.html.twig
// #2 Current Template: side.html.twig
// #3 Current Template: base.html.twig
// #4 Current Template: child.html.twig
}