Thanks a lot Nathan.

---------- Forwarded message ----------
From: Nathan Kerr
Date: Thu, Jul 13, 2017 at 3:48 AM
Subject: Re: [go-nuts] Template func ParseFiles, parsing multiple files
To: Tong Sun


MyTempl will have different templates after the second call. The template
called “tf1” from the first call will be replaced with the template found
in “tf1” from the second call.

MyTempl.Execute() will use the name of the first template that was parsed,
in this case “tf1"


On Jul 6, 2017, at 21:59, Tong Sun <suntong...@gmail.com> wrote:

Thanks Nathan.

I'm still confused.

>* ParseFiles adds a new template to the collection with the filename as
its name*

So if my MyTempl was named "tf1", calling MyTempl.ParseFiles() with more
than one files will have new templates created according to each of the
passed filename? Or the template name never changes regardless how many
files are passed in `ParseFiles()`?

I.e., for

 MyTempl := tt.New("tf1")

MyTempl.ParseFiles("tf1")
MyTempl.ParseFiles("tf1","tf2")

Would MyTempl have different template names after above each statement? If
so, then which one does the `MyTempl.Execute()` uses?


On Thursday, July 6, 2017 at 1:49:54 PM UTC-4, Nathan Kerr wrote:
>
> A text.Template contains one or more named templates. ParseFiles adds a
> new template to the collection with the filename as its name. Template
> names are used to tell Template.ExecuteTemplate
> <https://godoc.org/text/template#Template.ExecuteTemplate> which of the
> included templates to execute.
>
> On Thursday, July 6, 2017 at 6:37:01 AM UTC+2, Tong Sun wrote:
>>
>> What would it happen if I pass two or more files to:
>>
>> func (*Template) ParseFiles
>> <https://golang.org/src/text/template/helper.go#L52>
>> ❖ <https://golang.org/src/text/template/helper.go#L52>
>>
>> func (t *Template <http://godoc.org/text/template#Template>) 
>> ParseFiles(filenames ...string <http://godoc.org/builtin#string>) (*Template 
>> <http://godoc.org/text/template#Template>, error 
>> <http://godoc.org/builtin#error>)
>>
>> ParseFiles parses the named files and associates the resulting templates
>> with t. If an error occurs, parsing stops and the returned template is nil;
>> otherwise it is t. There must be at least one file. Since the templates
>> created by ParseFiles are named by the base names of the argument files, t
>> should usually have the name of one of the (base) names of the files. If it
>> does not, depending on t's contents before calling ParseFiles, t.Execute
>> may fail. In that case use t.ExecuteTemplate to execute a valid template.
>>
>> When parsing multiple files with the same name in different directories,
>> the last one mentioned will be the one that results.
>>
>> E.g.,
>>
>> What would the differences be that affects the output, for
>>
>> MyTempl.ParseFiles(tf1)
>>
>> vs.
>>
>> MyTempl.ParseFiles(tf1,tf2)?
>>
>> Will the content of tf2 be appended to tf1?
>>
>> Thx
>>
>>
-- 
You received this message because you are subscribed to a topic in the
Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/
topic/golang-nuts/LGJJdrHsD78/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

Reply via email to