-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_function_test.go
114 lines (91 loc) · 2.56 KB
/
template_function_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
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"text/template"
)
// Template Function
type MyPgae struct {
Name string
}
func (m MyPgae) SayHello(name string) string {
return "Hello " + name + ", My Name Is " + m.Name
}
func templateFunction(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.ParseFiles("./templates/mypage.html"))
t.ExecuteTemplate(w, "mypage", MyPgae{
Name: "Akbar",
})
}
func TestTemplateFunction(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, "localhost:1417", nil)
recorder := httptest.NewRecorder()
templateFunction(recorder, request)
resp := recorder.Result()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
// Global Function
func templateFunctionGlobal(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.New("glob").ParseFiles("./templates/global_func.html"))
t.ExecuteTemplate(w, "glob", map[string]interface{}{
"Name": "Jalaluddin Muh Akbar",
})
}
func TestTemplateGlob(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "localhost:1417", nil)
rec := httptest.NewRecorder()
templateFunctionGlobal(rec, req)
resp := rec.Result()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
// Menambah Global Function
func templateAddFunction(w http.ResponseWriter, r *http.Request) {
parse := `{{upper .Name}}`
data := MyPgae{Name: "akbar"}
t := template.New("addFunc")
// adding func
t.Funcs(map[string]interface{}{
"upper": func(s string) string {
return strings.ToUpper(s)
},
})
t = template.Must(t.Parse(parse))
t.ExecuteTemplate(w, "addFunc", data)
}
func TestTemplateAddFunc(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "localhost:1417", nil)
rec := httptest.NewRecorder()
templateAddFunction(rec, req)
resp := rec.Result()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
// Pipelines
func templateFunctionPipelines(w http.ResponseWriter, r *http.Request) {
t := template.New("pipelines")
t = t.Funcs(map[string]interface{}{
"sayHello": func(name string) string {
return "Hello " + name
},
"upper": func(upper string) string {
return strings.ToUpper(upper)
},
})
var parse = `{{sayHello .Name | upper}}`
t = template.Must(t.Parse(parse))
t.ExecuteTemplate(w, "pipelines", MyPgae{Name: "Akbar"})
}
func TestTemplatePipelines(t *testing.T) {
req := httptest.NewRequest("GET", "localhost:1417", nil)
rec := httptest.NewRecorder()
templateFunctionPipelines(rec, req)
resp := rec.Result()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}