variable scope in nested go templates

3,577 views
Skip to first unread message

RoboTamer

unread,
Aug 27, 2012, 4:11:48 AM8/27/12
to golan...@googlegroups.com

I been on this way too long, and was wondering if I could get some help please

My variables are only available in the template that get's executed with ExecuteTemplate. 
If I don't nest and only execute the view template that is without the layout template it works fine.
But when I nest the variable scope seams to be limited to the layout.html template

As example, I have a var "Title" it shows up at the template that gets executed but not in the nested template

How do I fix this?


func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {

    t, err := template.ParseFiles(ROOT+"/gui/layout.html", ROOT+"/gui/wiki/"+tmpl+".html")
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }

    err = t.ExecuteTemplate(w, "layout.html", p)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}


{{define "Content"}}
<h1>Editing {{.Title}}</h1>
<p>[<a href="/wiki/edit/{{.Title}}">edit</a>]</p>
<div>{{printf "%s" .Body}}</div>
{{end}}


Jesse McNelis

unread,
Aug 27, 2012, 5:44:03 AM8/27/12
to RoboTamer, golan...@googlegroups.com
On Mon, Aug 27, 2012 at 6:11 PM, RoboTamer <grue...@gmail.com> wrote:
> My variables are only available in the template that get's executed with
> ExecuteTemplate.
> If I don't nest and only execute the view template that is without the
> layout template it works fine.
> But when I nest the variable scope seams to be limited to the layout.html
> template

You have to pass the variables to the nested template.
"{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline."
http://golang.org/pkg/text/template



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

RoboTamer

unread,
Aug 27, 2012, 7:19:46 AM8/27/12
to golan...@googlegroups.com, RoboTamer, jes...@jessta.id.au
Thanks  Jesse that worked.

For those who are not familiar with Linux.

pipe in Linux is to send the output from one command to the next.
such as:  ls -al *.go | grep i
That would list all go files (ls is like dir in dos) but rather then displaying the output it would pipe it over the |   to the grep command. The grep command then finds all with an i in the name and the result gets displayed.

In Linux the pipe is: | and in go it is . (a dot)

So all I had to do is change {{template "Content"}} to {{template "Content" . }} notice the dot



Jesse McNelis

unread,
Aug 27, 2012, 8:08:28 AM8/27/12
to RoboTamer, golan...@googlegroups.com
On Mon, Aug 27, 2012 at 9:19 PM, RoboTamer <grue...@gmail.com> wrote:
> In Linux the pipe is: | and in go it is . (a dot)

That's not correct.
dot is the "cursor variable", like a cursor in a text editor.

"Templates are executed by applying them to a data structure.
Annotations in the template refer to elements of the data structure
(typically a field of a struct or a key in a map) to control execution
and derive values to be displayed. Execution of the template walks the
structure and sets the cursor, represented by a period '.' and called
"dot", to the value at the current location in the structure as
execution proceeds."
eg.
<h1>Editing {{.Title}}</h1>
is getting the 'Title' field from the cursor variable.

> So all I had to do is change {{template "Content"}} to {{template "Content" . }}
Here you are passing the cursor variable to the "Content" template.

http://golang.org/pkg/text/template/





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

dsan...@medallia.com

unread,
Jul 14, 2017, 4:12:57 PM7/14/17
to golang-nuts, grue...@gmail.com
This is the right answer and a good explanation. Thanks. 
Reply all
Reply to author
Forward
0 new messages