當我嘗試使用Funcs和時,它注意到Go模板很奇怪FuncMap。下面的代碼按預期工作:buffer := bytes.NewBufferString("")funcMap := template.FuncMap{ "label": strings.Title,}t, _ := template.New("alex").Funcs(funcMap).Parse("{{label \"alex\"}}") t.Execute(buffer, "")return string(buffer.Bytes()) //=> "Alex"但是,當我嘗試將模板放在文件中時,它不起作用(Execute()說:)"alex" is an incomplete or empty template:t, _ := template.New("alex").Funcs(funcMap).ParseFiles("template.html") 使用template.html:{{label \"alex\"}}知道為什么嗎?這是一個錯誤嗎?有沒有更簡單的方法在模板中使用方法/功能?
轉到模板功能
慕桂英3389331
2021-04-30 10:11:20