Golang 模板(并将函数传递给模板)
Golang templates (and passing funcs to template)
当我尝试访问传递给模板的函数时出现错误:
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
}
这是一个生成结构样板代码的程序(如果有人想知道我为什么在我的模板中这样做)。
在解析模板之前需要注册自定义函数,否则解析器将无法判断标识符是否是有效的函数名称。模板设计为可静态分析,这是对此的要求。
您可以先使用 template.New()
, and besides the template.ParseFiles()
function, the template.Template
type (returned by New()
) also has a Template.ParseFiles()
方法 创建一个新的未定义模板,您可以调用它。
像这样:
t, err := template.New("").Funcs(template.FuncMap{
"makeGoName": makeGoName,
"makeDBName": makeDBName,
}).ParseFiles("templates/struct.tpl")
请注意,template.ParseFiles()
函数还在后台调用 template.New()
,将第一个文件的名称作为模板名称传递。
也Template.Execute()
returns an error
,打印出来看看是否没有输出,例如:
if err := t.Execute(os.Stdout, data); err != nil {
fmt.Println(err)
}
为您的模板注册自定义函数并使用时 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
.
当我尝试访问传递给模板的函数时出现错误:
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
}
这是一个生成结构样板代码的程序(如果有人想知道我为什么在我的模板中这样做)。
在解析模板之前需要注册自定义函数,否则解析器将无法判断标识符是否是有效的函数名称。模板设计为可静态分析,这是对此的要求。
您可以先使用 template.New()
, and besides the template.ParseFiles()
function, the template.Template
type (returned by New()
) also has a Template.ParseFiles()
方法 创建一个新的未定义模板,您可以调用它。
像这样:
t, err := template.New("").Funcs(template.FuncMap{
"makeGoName": makeGoName,
"makeDBName": makeDBName,
}).ParseFiles("templates/struct.tpl")
请注意,template.ParseFiles()
函数还在后台调用 template.New()
,将第一个文件的名称作为模板名称传递。
也Template.Execute()
returns an error
,打印出来看看是否没有输出,例如:
if err := t.Execute(os.Stdout, data); err != nil {
fmt.Println(err)
}
为您的模板注册自定义函数并使用时 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
.