Error: template: function not defined

2,451 views
Skip to first unread message

RoboTamer

unread,
Sep 4, 2012, 10:04:37 PM9/4/12
to golan...@googlegroups.com

Following code gives me the error function "lower" not defined any idea why ?
Everything works fine without the lower label in the template  

type View struct {
    Func template.FuncMap
}


var V = new(View)

func main() {
    V.Func = template.FuncMap{
        "lower": strings.ToLower,
    }
}

func genTemplate(w http.ResponseWriter, v *View) {

}

template looks like this:

{{range .Menu}}<a href="/wiki/{{lower .}}" title="{{.}}"> {{.}}</a>{{end}}

Jesse McNelis

unread,
Sep 4, 2012, 10:12:35 PM9/4/12
to RoboTamer, golan...@googlegroups.com
On Wed, Sep 5, 2012 at 12:04 PM, RoboTamer <grue...@gmail.com> wrote:
> Following code gives me the error function "lower" not defined any idea why
> ?
> Everything works fine without the lower label in the template

When you post code for help you should include complete examples that
show the issues you're having.
The problem with posting vague parts of your code is that if you don't
know what the problem is then you also don't know what parts
of the code are relevant to the problem.

From what you posted I guessyou're not passing the template.FuncMap
you've created to the template you want to render.
http://golang.org/pkg/text/template/#Template.Funcs

But since you don't show that part of your code, I can't be much more helpful.


> type View struct {
>
> Func template.FuncMap
>
> }
>
>
> var V = new(View)
>
>
> func main() {
>
> V.Func = template.FuncMap{
>
> "lower": strings.ToLower,
>
> }
>
> }
>
>
> func genTemplate(w http.ResponseWriter, v *View) {
>
>
> }
>
>
> template looks like this:
>
>
> {{range .Menu}}<a href="/wiki/{{lower .}}" title="{{.}}"> {{.}}</a>{{end}}
>
>



--
=====================
http://jessta.id.au

RoboTamer

unread,
Sep 4, 2012, 10:58:11 PM9/4/12
to golan...@googlegroups.com, RoboTamer, jes...@jessta.id.au
Okay
I have two functions just uncomment to test

//fails(V)
works(V)

RoboTamer

unread,
Sep 4, 2012, 11:04:02 PM9/4/12
to golan...@googlegroups.com, RoboTamer, jes...@jessta.id.au

This one is a little better:
Can't modify the earlier one so I am posting a new one

Jesse McNelis

unread,
Sep 4, 2012, 11:33:57 PM9/4/12
to RoboTamer, golan...@googlegroups.com
On Wed, Sep 5, 2012 at 1:04 PM, RoboTamer <grue...@gmail.com> wrote:
>
> This one is a little better:
> http://play.golang.org/p/jJYrRYA7wX
> Can't modify the earlier one so I am posting a new one

Yep, you have to set the func map for the template.

eg.
func fails(V *View) {
t := template.New("lower template")

// set funcmap for template.
t.Funcs(V.Func)

t, err := t.Parse("<a href=\"/wiki/{{lower .Name}}\"
title=\"{{.Name}}\"> {{.Name}}")
if err != nil {
panic(err)
}
err = t.Execute(os.Stdout, V)
if err != nil {
panic(err)
}
}

http://play.golang.org/p/TMpx7nYva3
--
=====================
http://jessta.id.au

RoboTamer

unread,
Sep 5, 2012, 3:12:04 AM9/5/12
to golan...@googlegroups.com, RoboTamer, jes...@jessta.id.au
Thanks Jesse, that was it.
Reply all
Reply to author
Forward
0 new messages