I've the below that is working fine playground
package main
import (
"html/template"
"os"
)
func main() {
tmpl := `
{{ $slice := mkSlice "a" 5 "b" }}
{{ range $slice }}
{{ . }}
{{ end }}
`
funcMap := map[string]interface{}{"mkSlice": mkSlice}
t := template.New("").Funcs(template.FuncMap(funcMap))
template.Must(t.Parse(tmpl))
t.Execute(os.Stdout, nil)
}
func mkSlice(args ...interface{}) []interface{} {
return args
}
But once I tried to run in from template file, nothing had been displayed, and no error had been recieved!
func mkSlice(args ...interface{}) []interface{} { // to ceate the array in the template
return args
}
funcMap := map[string]interface{}{"mkSlice": mkSlice}
tmpl := template.New("").Funcs(template.FuncMap(funcMap))
template.Must(tmpl.ParseFiles("index.html"))
tmpl.Execute(w, nil)
And index.html is:
{{ $slice := mkSlice "a" 5 "b" }}
{{ range $slice }}
<span> {{ . }} </span>
{{ end }}
any thought?