Golang templates (and passing funcs to template)

12,092

Solution 1

Custom functions need to be registered before parsing the templates, else the parser would not be able to tell whether an identifier is a valid function name or not. Templates are designed to be statically analyzable, and this is a requirement to that.

You can first create a new, undefined template with template.New(), and besides the template.ParseFiles() function, the template.Template type (returned by New()) also has a Template.ParseFiles() method, you can call that.

Something like this:

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

Note that the template.ParseFiles() function also calls template.New() under the hood, passing the name of the first file as the template name.

Also Template.Execute() returns an error, print that to see if no output is generated, e.g.:

if err := t.Execute(os.Stdout, data); err != nil {
    fmt.Println(err)
}

Solution 2

When registering custom functions for your template and using ParseFiles() you need to specify the name of the template when instantiating it and executing it. You also need to call ParseFiles() after you call Funcs().

// 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)
}

When working with named templates the name is the filename without the directory path e.g. struct.tpl not templates/struct.tpl. So the name in New() and ExecuteTemplate() should be the string struct.tpl.

Share:
12,092

Related videos on Youtube

b0xxed1n
Author by

b0xxed1n

Updated on June 25, 2022

Comments

  • b0xxed1n
    b0xxed1n almost 2 years

    I'm getting an error when I try and access a function I'm passing to my template:

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

    Can anyone please let me know what I'm doing wrong?

    Template file (struct.tpl):

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

    Calling file:

    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
    }
    

    This is for a program that generates struct boilerplate code (in case anyone is wondering why I'm doing that in my template).

    • Volker
      Volker about 8 years
      You need to add the functions (i.e. calling t.Funcs) before parsing with ParseFiles. To do this get a fresh template via New(), then add your funcs than parse the files into it. (Untested)
  • b0xxed1n
    b0xxed1n about 8 years
    Thank you, that was very clear. I'm not getting any errors now, but strangely there's no output. I outputted it to a bytes.Buffer to test that it is indeed empty, and the bytes.Buffer is also empty. Any idea why that might be, or how I can debug this when I have no output or errors?
  • pepe
    pepe about 7 years
    Pass the "struct.tpl" string to the New function. From the doc: Since the templates created by ParseFiles are named by the base names of the argument files, t should usually have the name of one of the (base) names of the files. If it does not, depending on t's contents before calling ParseFiles, t.Execute may fail. In that case use t.ExecuteTemplate to execute a valid template.
  • frankgreco
    frankgreco over 5 years
    @PéterSzakszon your comment is actually required. Otherwise, you'll get an error that template: : "" is an incomplete or empty template
  • gumelaragum
    gumelaragum over 3 years
    template.New(""), "" need to be defined, if not this will make error template: : "" is an incomplete or empty template, and the Name must equal to filename
  • icza
    icza over 3 years
    @gumelaragum Yes, if you parse the template text from a string. But in my examples I don't do that, I call Template.ParseFiles().