given that you can already call methods on
objects with dynamically known type, i don't see that
there's any particular security reason to disallow this,
but there may be other reasons i haven't thought of
perhaps you might raise an issue.
yes, you can do it by explicitly naming the function,
but you can't dynamically choose a function to call,
which might be useful (you can make a map look like a type with
methods this way, for example).
package main
import (
"fmt"
"os"
"template"
)
func foo(s string) string {
return "foo: " + s
}
func main() {
t := template.Must(template.New("name").Parse(`{{.F "arg"}}`))
v := struct{F func(string) string}{foo}
e := t.Execute(os.Stdout, v)
fmt.Printf("returned err %v\n", e)
}
Is there a other way to put dynamic content to the template....
The ideale Is to add html snippets by functions Into the template
if you can decide on a single signature for your snippet functions,
then it's easy. for instance:
package main
import (
"fmt"
"os"
"template"
)
type Snippet func(arg string) string
func (s Snippet) Call(arg string) string {
return s(arg)
}
func foo(s string) string {
return "foo: " + s
}
func main() {
t := template.Must(template.New("name").Parse(`{{.F.Call "arg"}}`))
v := struct{F Snippet}{foo}
e := t.Execute(os.Stdout, v)
fmt.Printf("\nreturned err %v\n", e)
}