difference between template.Execute and template.ExecuteTemplate()

1,706 views
Skip to first unread message

mir.an...@gmail.com

unread,
Sep 9, 2016, 8:18:44 AM9/9/16
to golang-nuts
Hi,

func (t *Template) Execute(wr io.Writer, data interface{}) error
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

Can someone tell me the difference between the above two functions ?

Asit Dhal

unread,
Sep 9, 2016, 9:05:45 AM9/9/16
to mir.an...@gmail.com, golang-nuts

In case of second one, in the name string parameter, you need to specify the template name.

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Warm Regards,
Asit Dhal
http://asit-dhal.github.io/

Val

unread,
Sep 9, 2016, 5:50:58 PM9/9/16
to golang-nuts, mir.an...@gmail.com
Hello
It seems that the relevant part of the doc is here :
Each template is named by a string specified when it is created. Also, each template is associated with zero or more other templates that it may invoke by name; such associations are transitive and form a name space of templates.

So, a *Template always has a name, whether explicitly set by user calling New(name), or automatically assigned by ParseFiles or ParseGlob. Also, you can have nested template definitions, which create additional template names.
At first I thought a *Template would "contain" one or more named "things", but the doc suggests that the right terminology would be "a template may have associated templates".

I wrote this example showing that Execute / ExecuteTemplate give different results, for different names invoked :
Execute
ExecuteTemplate "phase1"
ExecuteTemplate "phase2"
ExecuteTemplate "phase3"
ExecuteTemplate "wrongName"

Cheers
 Val
Reply all
Reply to author
Forward
0 new messages