gpt4 book ai didi

html - 在 html 模板中找不到自定义模板函数

转载 作者:数据小太阳 更新时间:2023-10-29 03:21:02 25 4
gpt4 key购买 nike

<分区>

我以这种方式呈现模板:

  func renderTemplate(...........) {
rt := template.Must(template.ParseFiles(
fmt.Sprintf("%s/%s", templatesPath, baseLayoutPath),
fmt.Sprintf("%s/%s", templatesPath, tplName)))

err := rt.ExecuteTemplate(w, "base", nil)
//[................]
}

我想在其中注册一个自定义函数:

func myTest1(string s) string {
return "hello: " + s
}


func renderTemplate(...........) {
rt := template.Must(template.ParseFiles(
fmt.Sprintf("%s/%s", templatesPath, baseLayoutPath),
fmt.Sprintf("%s/%s", templatesPath, tplName))).Funcs(template.FuncMap{"test1": myTest1})

这不起作用:“test1”未定义”

  // html template:


{{range .items}}

{{.Field1 | test1}}

为什么不,如何让它发挥作用?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com