Commit 790c842f authored by Tw's avatar Tw

template: add test for custom function

Signed-off-by: default avatarTw <tw19881113@gmail.com>
parent f28a159b
......@@ -264,8 +264,7 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin
return "", err
}
tpl := template.New(filename).Funcs(TemplateFuncs)
tpl, err = tpl.Parse(string(body))
tpl, err := template.New(filename).Funcs(TemplateFuncs).Parse(string(body))
if err != nil {
return "", err
}
......
......@@ -72,8 +72,16 @@ func TestInclude(t *testing.T) {
shouldErr: true,
expectedErrorContent: `type httpserver.Context`,
},
// Test 4 - all good, with custom function
{
fileContent: `hello {{ caddy }}`,
expectedContent: "hello caddy",
shouldErr: false,
expectedErrorContent: "",
},
}
TemplateFuncs["caddy"] = func() string { return "caddy" }
for i, test := range tests {
testPrefix := getTestPrefix(i)
......
......@@ -121,6 +121,8 @@ func TestTemplates(t *testing.T) {
rec = httptest.NewRecorder()
// register custom function which is used in template
httpserver.TemplateFuncs["root"] = func() string { return "root" }
tmplroot.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
......
<!DOCTYPE html><html><head><title>root</title></head><body>{{.Include "header.html"}}</body></html>
<!DOCTYPE html><html><head><title>{{ root }}</title></head><body>{{.Include "header.html"}}</body></html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment