This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtemplate.my
120 lines (96 loc) · 3.04 KB
/
template.my
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
resultValue = ""
//test using array
arr = [
{ "key" : "key1", "value" : "value1" },
{ "key" : "key2", "value" : "value2" },
{ "key" : "key3", "value" : "value3" }
]
//use parseFiles(), write to a string
template.parseTextFiles("./examples/looping.tmpl").execute(resultValue, arr)
println('{resultValue}')
//use parseFiles(), write to a file
file = newFile("./examples/outTemplate.log", "a+")
template.parseTextFiles("./examples/looping.tmpl").execute(file, arr)
//use parse()
//Note here: we need to use "{{-" and "-}}" to remove the newline from the output
template.newText("array").parse(`Looping
{{- range . }}
key={{ .key }}, value={{ .value -}}
{{- end }}
`).execute(resultValue, arr)
println('{resultValue}')
//test using hash
hs = { "key" : "key1", "value" : "value1" }
template.newText("hash").parse(`key={{.key }}, value={{ .value -}}`).execute(resultValue, hs)
println('{resultValue}')
//test using integer
iValue = 15
template.newText("int").parse(`value={{.}}`).execute(resultValue, iValue)
println('{resultValue}')
//test using float
fValue = 15.2
template.newText("float").parse(`value={{.}}`).execute(resultValue, fValue)
println('{resultValue}')
//test using boolean
bValue = true
template.newText("bool").parse(`value={{.}}`).execute(resultValue, bValue)
println('{resultValue}')
//test using time
tValue = newTime()
template.newText("time").parse(`value={{.}}`).execute(resultValue, tValue)
println('{resultValue}')
//test using string
sValue = "world"
template.newText("string").parse(`value={{.}}
value={{printf "%q" . -}}
`).execute(resultValue, sValue)
println('{resultValue}')
//test using Nil
nValue = nil
template.newText("Nil").parse(`value={{.}}`).execute(resultValue, nValue)
println('{resultValue}')
//test template.funcs()
templateText = `
Input: {{printf "%q" .}}
Output 0: {{title .}}
Output 1: {{title . | printf "%q"}}
Output 2: {{printf "%q" . | title}}
Output 3: {{upper .}}
Output 4: {{upper . | printf "%q"}}
Output 5: {{printf "%q" . | upper}}
`
template.newText("titleTest").funcs(
{
"title" : fn(x){strings.title(x)},
"upper" : fn(x){strings.upper(x)}
}
).parse(templateText).execute(resultValue, "the go programming language")
println('{resultValue}')
envText = `ENVIRONMENT PATH: {{getenv .}}`
template.newText("titleTest").funcs(
{
"getenv" : fn(x){os.getenv(x)}
}
).parse(envText).execute(resultValue, "PATH")
println('{resultValue}')
/////////////////////////////////
// HTML Template Test
/////////////////////////////////
guardians = ["Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"]
master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
overlay = `{{define "list"}} {{join . ", "}}{{end}} `
masterTmpl = template.newHtml("master").funcs(
{
"join" : fn(x,y) { strings.join(x,y) }
}
).parse(master)
overlayTmpl = masterTmpl.clone().parse(overlay)
ret = masterTmpl.execute(stdout, guardians)
if ret == false {
printf("Parse failed, reason:%s\n", ret.message())
}
ret = overlayTmpl.execute(stdout, guardians)
if ret == false {
printf("Parse failed, reason:%s\n", ret.message())
}
println()