Golang模板(以及向模板传递函数)

14

当我尝试访问我传递给模板的函数时,出现了一个错误:

Error: template: struct.tpl:3: function "makeGoName" not defined

请问我做错了什么?

模板文件(struct.tpl):

type {{.data.tableName}} struct {
  {{range $key, $value := .data.tableData}}
  {{makeGoName $value.colName}} {{$value.colType}} `db:"{{makeDBName $value.dbColName}},json:"{{$value.dbColName}}"`
  {{end}}
}

调用文件:

type tplData struct {
    tableName string
    tableData interface{}
}

func doStuff() {
    t, err := template.ParseFiles("templates/struct.tpl")
    if err != nil {
        errorQuit(err)
    }

    t = t.Funcs(template.FuncMap{
        "makeGoName": makeGoName,
        "makeDBName": makeDBName,
    })

    data := tplData{
        tableName: tableName,
        tableData: tableInfo,
    }

    t.Execute(os.Stdout, data)
}

func makeGoName(name string) string {
    return name
}

func makeDBName(name string) string {
    return name
}

这是一个生成结构体样板代码的程序(如果有人想知道我为什么要在我的模板中这样做)。


1
在使用ParseFiles解析之前,您需要添加函数(即调用t.Funcs)。为此,请通过New()获取一个新的模板,然后添加您的函数,最后将文件解析到其中。(未经测试) - Volker
2个回答

27

在解析模板之前,需要先注册自定义函数,否则解析器无法确定标识符是否为有效的函数名称。模板设计为可以静态分析,并且这是必要的要求。

您可以首先使用 template.New() 创建一个新的未定义模板,除了template.ParseFiles()函数外, template.Template 类型(由New()返回)还有一个Template.ParseFiles()方法,您可以调用它。

像这样:

t, err := template.New("").Funcs(template.FuncMap{
    "makeGoName": makeGoName,
    "makeDBName": makeDBName,
}).ParseFiles("templates/struct.tpl")

请注意,template.ParseFiles()函数在内部也调用了template.New()函数,并将第一个文件的名称作为模板名称传递。
此外,Template.Execute()函数返回一个error,如果没有生成输出,请打印该错误信息。例如:
if err := t.Execute(os.Stdout, data); err != nil {
    fmt.Println(err)
}

2
谢谢,讲得非常清楚。现在我没有收到任何错误信息,但奇怪的是没有输出。我将其输出到一个bytes.Buffer中以测试它是否为空,而bytes.Buffer也为空。有什么想法为什么会这样,或者当我没有输出或错误时如何调试它? - b0xxed1n
9
将字符串"struct.tpl"传递给New函数。从文档中可以看到,由ParseFiles创建的模板以参数文件的基本名称命名,因此t通常应该具有文件(基本)名称之一的名称。如果没有这样做,则根据调用ParseFiles之前t的内容,t.Execute可能会失败。在这种情况下,请使用t.ExecuteTemplate来执行有效的模板。 - pepe
4
@PéterSzakszon,您的评论是必需的。否则,您将会收到一个错误提示:“template: : "" 是一个不完整或空白的模板”。 - frankgreco
1
template.New(""),需要定义"",否则会出现错误template: : "" is an incomplete or empty template,且名称必须等于文件名。 - gumelaragum
@gumelaragum 是的,如果你从一个string中解析模板文本的话。但是在我的例子中,我没有这样做,我调用了Template.ParseFiles() - icza

7

当为您的模板注册自定义函数并使用 ParseFiles() 时,您需要在实例化和执行模板时指定模板的名称。您还需要在调用 Funcs() 后调用 ParseFiles()

// Create a named template with custom functions
t, err := template.New("struct.tpl").Funcs(template.FuncMap{
    "makeGoName": makeGoName,
    "makeDBName": makeDBName,
}).ParseFiles("templates/struct.tpl") // Parse the template file
if err != nil {
    errorQuit(err)
}

// Execute the named template
err = t.ExecuteTemplate(os.Stdout, "struct.tpl", data)
if err != nil {
    errorQuit(err)
}

使用命名模板时,名称是不带目录路径的文件名,例如struct.tpl而不是templates/struct.tpl。因此,在New()ExecuteTemplate()中的名称应该是字符串struct.tpl


1
不确定为什么Go开发人员会采用这种模式,但这种模式似乎是违反直觉的。例如,如果我从命令行作为标志获取用户的模板路径,比如说“templates/index.html”,我必须从路径中提取文件名“index.html”,以便稍后执行HTML生成。 - ratulotron

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