使用Golang从Base64字符串创建图像 ZgotmplZ

3

我试图将base64格式的图像放入 `` 标签中,但是出现了 ZgotmplZ 的错误。我尝试使用类似于 template.URL(s) 的方法:

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())
})

在模板中:
 <td rowspan="2"><img src="{{.Icon|safe}}"></td>

但是当我执行:go run时,出现了以下错误:

panic: template: index.html:34: function "safe" not defined

那么我做错了什么?

1个回答

3

在解析模板之前必须定义模板函数。为了修复此错误,创建一个空模板,设置函数,然后解析全局变量:

templates: template.Must(template.New("").Funcs(funcMap).ParseGlob("C:/Projects/Golang/hello/resources/*.html")),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接