Let's say I have
type Person struct {
Name string
}
func (p *Person) Label() string {
return "This is " + p.Name
}
How can I use this method from a html/template ? I would need something like this in my template:
{{ .Label() }}
Let's say I have
type Person struct {
Name string
}
func (p *Person) Label() string {
return "This is " + p.Name
}
How can I use this method from a html/template ? I would need something like this in my template:
{{ .Label() }}
Just omit the parentheses and it should be fine. Example:
According to the documentation, you can call any method which returns one value (of any type) or two values if the second one is of type
error
. In the later case,Execute
will return that error if it is non-nil and stop the execution of the template.You can even pass parameters to function like follows
And then in the template write
Assuming that the person in the template is a variable of type Person passed to Template.