DDanya
DDanya

Reputation: 33

How to use parse many times in golang's template

i need help. Now to output pages I use multiple templates(1 example) and i want to use parse many times in one template(2 example)

Example 1:

...
t, err := template.ParseFiles("header.html")
t.Execute(wr, data)
d, err := template.ParseFiles("content.html")
d.Execute(wr, datatwo)
...

Example 2:

...
t := template.New("blabla")
t.ParseFiles("header.html")
t.ParseFiles("content.html")
t.Execute("wr", data)

P.S. I'm sorry, my english is very bad

Upvotes: 0

Views: 1043

Answers (1)

elithrar
elithrar

Reputation: 24300

template.ParseFiles can take multiple filenames as an argument, as per http://golang.org/pkg/html/template/#ParseFiles

func ParseFiles(filenames ...string) (*Template, error)

e.g.

t := template.New("base")
t, err := t.ParseFiles("header.html", "footer.html", "content.html")
if err != nil {
    // handle the error
}

There's a solid example on how to use html/template in the Go docs as well: http://golang.org/doc/articles/wiki/#tmp_6

Upvotes: 1

Related Questions