Skip to content

text/template: {{call}} doesn't pass parameters to struct methods #10653

@joncalhoun

Description

@joncalhoun

Go version: go version go1.4.2 darwin/amd64
OS: OS X Yosemite Version 10.10.1

When using {{call ...}} in a template with a struct method the template parser fails to send any arguments to the method. This does work correctly if the method returns a function, or if the function you want to call is set to an attribute.

If this isn't intended to work, the error message could be a little clearer.

This can be replicated on the Go Playground here: https://play.golang.org/p/Ka8bN3_V1f and the source from the link is below.

package main

import (
    "os"
    "text/template"
)

type TestStructA struct {
    Test func(string) string
}

func test(s string) string {
    return "Test A Says " + s + "\n"
}

type TestStructB struct {
}

func (self TestStructB) Test() func(string) string {
    return func(s string) string { return "Test B Says " + s + "\n" }
}

// DOES NOT WORK
type TestStructC struct {
}

func (self TestStructC) Test(s string) string {
    return "Test C Says " + s + "\n"
}

func main() {
    tempStr := "{{call .Test \"hi\"}}"
    t := template.Must(template.New("test_temp").Parse(tempStr))

    testA := TestStructA{Test: test}
    err := t.Execute(os.Stdout, testA)
    if err != nil {
        panic(err)
    }

    testB := TestStructB{}
    err = t.Execute(os.Stdout, testB)
    if err != nil {
        panic(err)
    }

    // DOES NOT WORK
    testC := TestStructC{}
    err = t.Execute(os.Stdout, testC)
    if err != nil {
        panic(err)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions