0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

template: add test for custom function

Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
Tw 2017-04-18 22:49:20 +08:00
parent f28a159b72
commit 790c842fad
4 changed files with 12 additions and 3 deletions

View file

@ -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
}

View file

@ -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)

View file

@ -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 {

View file

@ -1 +1 @@
<!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>