3

So i try to put image in from base64 but i'm gettin ZgotmplZ i try to use template.URL(s) like this:

e := echo.New()
funcMap := template.FuncMap{
    "safe": func(s string) template.URL {
        return template.URL(s)
    },
}
t := &Template{
    templates: template.Must(template.ParseGlob("C:/Projects/Golang/hello/resources/*.html")).Funcs(funcMap),
}
e.Renderer = t
e.GET("/", func(context echo.Context) error {
    return context.Render(http.StatusOK, "index.html", GetData())
})

and in template:

 <td rowspan="2"><img src="{{.Icon|safe}}"></td>

But while i exeute: go run panic: template: index.html:34: function "safe" not defined so what i do wrong?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
dekros
  • 59
  • 1
  • 8

1 Answers1

3

The template functions must be defined before the template is parsed. To fix the error, create an empty template, set the functions and then parse the glob:

templates: template.Must(template.New("").Funcs(funcMap).ParseGlob("C:/Projects/Golang/hello/resources/*.html")),
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242