【发布时间】:2015-12-03 16:25:58
【问题描述】:
我使用以下代码来解析 html 模板。效果很好。
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
}
和html模板test.html。
<html>
<head>
<title></title>
</head>
<body>
<input type="text" value="{{add .A .B}}">
</body>
</html>
但是当我将 html 文件移动到另一个目录时。然后使用以下代码。输出始终为空。
t := template.New("./templates/test.html").Funcs(template.FuncMap{"add": add})
t, err := t.ParseFiles("./templates/test.html")
谁能告诉我怎么了?还是不能这样使用html/template包?
【问题讨论】:
标签: parsing templates dictionary go func