I am trying to write a template (using html / template) and pass it a structure to which some methods are bound, many of which return multiple values. Is there any way to access them from the template? I would like to be able to do something like:
package main import ( "fmt" "os" "text/template" ) type Foo struct { Name string } func (f Foo) Baz() (int, int) { return 1, 5 } const tmpl = `Name: {{.Name}}, Ints: {{$a, $b := .Baz}}{{$a}}, {{b}}` func main() { f := Foo{"Foo"} t, err := template.New("test").Parse(tmpl) if err != nil { fmt.Println(err) } t.Execute(os.Stdout, f) }
But obviously this will not work. Is there no way around this?
I considered creating an anonymous structure in my code:
data := struct { Foo a int b int }{ f, 0, 0, } data.a, data.b = f.Baz()
And passing it on, but would rather have something in the template. Any ideas? I also tried to write a wrapper function that I added to funcMaps, but I could never get it to work.
Thanks for any suggestions!
source share