【发布时间】:2018-12-29 17:41:47
【问题描述】:
我这样渲染模板:
func renderTemplate(...........) {
rt := template.Must(template.ParseFiles(
fmt.Sprintf("%s/%s", templatesPath, baseLayoutPath),
fmt.Sprintf("%s/%s", templatesPath, tplName)))
err := rt.ExecuteTemplate(w, "base", nil)
//[................]
}
我想在里面注册一个自定义函数:
func myTest1(string s) string {
return "hello: " + s
}
func renderTemplate(...........) {
rt := template.Must(template.ParseFiles(
fmt.Sprintf("%s/%s", templatesPath, baseLayoutPath),
fmt.Sprintf("%s/%s", templatesPath, tplName))).Funcs(template.FuncMap{"test1": myTest1})
这不起作用:"test1" not defined"
// html template:
{{range .items}}
{{.Field1 | test1}}
为什么不以及如何让它发挥作用?
【问题讨论】: