2017-11-25 119 views
0

所以我嘗試把圖像從的base64,但我想趁自己ZgotmplZ我嘗試 使用template.URL(S)是這樣的:Golang IMG

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> 

可是當我exeute:去運行, 恐慌:模板:index.html的:34:功能 「安全」 沒有定義 所以我做錯了什麼?

回答

0

模板函數必須在模板解析之前定義。若要修復該錯誤,請創建一個空模板,設置功能,然後解析該glob:

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

謝謝,現在它的工作,非常感謝 – dekros