I use following code to parse html template. It works well.
func test(w http.ResponseWriter, req *http.Request) {
data := struct {A int B int }{A: 2, B: 3}
t := template.New("test.html").Funcs(template.FuncMap{"add": add})
t, err := t.ParseFiles("test.html")
if err!=nil{
log.Println(err)
}
t.Execute(w, data)
}
func add(a, b int) int {
return a + b
}
and html template test.html.
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="{{add .A .B}}">
</body>
</html>
But when I move html file to another directory. Then use the following code. The output is always empty.
t := template.New("./templates/test.html").Funcs(template.FuncMap{"add": add})
t, err := t.ParseFiles("./templates/test.html")
Can anyone tell me what's wrong? Or html/template package can not be used like this?