You've got two options:

         t, err := template.ParseFiles("template.html")
         if nil!=err {
         t.Execute(os.Stdout, nil)

In this case, because the template is the result of .ParseFiles, the
template t holds the 'template.html' template.

On the other hand, you can also do this:

         t, err := template.New("").ParseFiles("template.html")
         if nil!=err {
         t.ExecuteTemplate(os.Stdout, "template.html", nil)

Here your template 't' will be named "", and the parsed files will be named
sub-templates, named after the _file name without directory_ : so a
template "templs/a.html" will be named "a.html".

If I just do 't.Execute', the template t will be executed, but it is empty.
So I need to name the template I want to execute - hence ExecuteTemplate.

Note in both cases the final parameter to .Execute or .ExecuteTemplate is
the data. I'm passing nil because my template doesn't need any data, but in
any real-world application you'd want to be adding data.

All the best,

You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 9 | next ›
Discussion Overview
groupgolang-nuts @
postedJun 27, '15 at 7:38p
activeJun 28, '15 at 11:36p



site design / logo © 2021 Grokbase