html/template function not defined

92 views
Skip to first unread message

alex.b...@leadinglocally.com

unread,
Jul 29, 2016, 10:32:12 PM7/29/16
to golang-nuts
func encodeJSON(data interface{}) template.JS {
a, err := json.Marshal(data)
if err != nil { panic(err) }

return template.JS(a)
}

// code for rendering ELEMENT

func (ele *ELEMENT) Render() ([]byte, error) {

// init map of functions available to use within delimiters

if funcMap == nil {
funcMap = template.FuncMap{
"divideFF": divideFF,
"percentageFF": percentageFF,
"percentageIF": percentageIF,
"encodeJSON": encodeJSON,
}
}

when executing only the encodeJSON function I get this: template: x:19: function "encodeJSON" not defined

all others work fine, and I checked the correct code is in my gopath.

please advise... I had the encodeJSON func returning a string before but it didn't work either

alex.b...@leadinglocally.com

unread,
Jul 30, 2016, 10:05:39 AM7/30/16
to golang-nuts

Since, I have made 2 more float-to-int functions and they work fine.

To solve this I'm having to pre-marshal my json before I execute the template.

Please if anyone knows why it doesn't like JSON encoding functions in the templates, let me know

Matt Silverlock

unread,
Aug 1, 2016, 9:48:31 AM8/1/16
to golang-nuts
Where is the code for parsing your templates and apply the FuncMap?
Reply all
Reply to author
Forward
0 new messages