I render templates this way:
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)
//[................]
}
I want to register a custom function in it:
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})
This doesn't work: "test1" not defined"
// html template:
{{range .items}}
{{.Field1 | test1}}
Why not and how to make it work?