Go Template ParseFiles函数可解析多个文件 [英] The Go Template ParseFiles func parsing multiple files

查看:102
本文介绍了Go Template ParseFiles函数可解析多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将两个或多个文件传递到Go Template的ParseFiles函数中会发生什么?

  func(* Template)ParseFiles 

它有助于说:

ParseFiles解析命名的文件,并将生成的模板与t关联.如果发生错误,则分析将停止,返回的模板为nil;否则,返回0.否则为t.必须至少有一个文件.由于由ParseFiles创建的模板是由参数文件的基本名称命名的,因此t通常应具有文件(基本)名称之一的名称.如果不是,则根据t的内容,在调用ParseFiles之前,t.Execute可能会失败.在这种情况下,请使用t.ExecuteTemplate执行有效的模板.

在不同目录中解析具有相同名称的多个文件时,最后提到的将是结果文件.

但是我仍然不确定会影响输出的差异是什么,因为

  MyTempl.ParseFiles(tf1) 

vs.

  MyTempl.ParseFiles(tf1,tf2) 

tf2 的内容追加到 tf1 的内容之后吗?

解决方案

首先介绍模板"概念:

template.Template 的值为已解析模板的表示形式" .但是这里的措辞有点不完善". template.Template 值可以是(通常是)多个相关模板的集合. template.Template 具有未导出的字段:

  tmpl map [string] * Template//从名称到定义模板的映射. 

tmpl 字段包含所有其他关联的模板,这些模板对于模板是可见的,并且可以通过其名称来引用.

您可以在以下答案中详细了解此内容:转到模板名称

返回 Template.ParseFiles() 方法.此方法从作为参数传递给它的文件中解析多个模板.从文件解析的模板将以文件名命名(没有文件夹,只有文件名),并将它们添加到方法指定的 t 模板的内部关联模板映射中接收器.

已解析的模板将不会被追加.将为它们创建多个单独的 template.Template 值,但它们将被关联(以便它们可以相互引用,例如它们可以彼此包含).

让我们看一个例子.假设我们有以下两个模板文件:

a.html 是:

 我是 

b.html :

 我是b. 

和示例代码:

  t:= template.New("a.html")如果_,err:= t.ParseFiles("a.html","b.html");err!= nil {恐慌}如果err:= t.Execute(os.Stdout,nil);err!= nil {恐慌} 

此示例创建一个名为 a.html 的新的空模板,然后解析2个文件: a.html b.html .

会有什么结果? t 将表示 a.html 模板,因为我们之前使用该特定名称创建了它.运行代码,输出将是:

 我是 

现在,如果我们将第一行更改为:

  t:= template.New("x.html") 

其余部分保持不变,运行它,我们会看到类似的内容:

  panic:模板:"x.html"是不完整或空的模板 

原因是 t 表示一个名为 x.html 的模板,但它是空的,因为我们没有将任何内容解析"入其中,而且解析的文件也与名称 x.html 不匹配.因此,尝试执行它会导致错误.

现在,如果我们尝试执行其关联的命名模板之一:

  if err:= t.ExecuteTemplate(os.Stdout,"a.html",nil);err!= nil {恐慌} 

成功,然后再次给出:

 我是 

What would it happen if I pass two or more files to Go Template's ParseFiles func?

func (*Template) ParseFiles

It helps says:

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.

But I'm still not sure 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 that of tf1?

First a little about the "template" concept:

A template.Template value is "the representation of a parsed template". But the wording here is a little "imperfect". A template.Template value may be (and usually is) a collection of multiple, associated templates. template.Template has an unexported field:

tmpl   map[string]*Template // Map from name to defined templates.

This tmpl field holds all other associated templates, templates that are visible to the template, and which can be referred to by their names.

You can read more about this in this answer: Go template name

Back to the Template.ParseFiles() method. This method parses multiple templates from the files passed to it as parameters. The templates parsed form the files will be named after the file names (without folders, just the file name), and they will be added to the internal, associated templates map of the t template designated by the method receiver.

The parsed templates will not be appended. Multiple, separate template.Template values will be created for them, but they will be associated (so they can refer to each other, e.g. they can include each other).

Let's see an example. Let's assume we have these 2 template files:

a.html is:

I'm a.

And b.html:

I'm b.

And an example code:

t := template.New("a.html")
if _, err := t.ParseFiles("a.html", "b.html"); err != nil {
    panic(err)
}
if err := t.Execute(os.Stdout, nil); err != nil {
    panic(err)
}

This example creates a new, empty template named a.html, then parses 2 files: a.html and b.html.

What will be the result? t will denote the a.html template, because we created it prior with that specific name. Running the code, the output will be:

I'm a.

Now if we change the first line to:

t := template.New("x.html")

And leave the rest unchanged, running it we see something similar:

panic: template: "x.html" is an incomplete or empty template

The reason is that t denotes a template named x.html but it's empty, as we didn't parse anything "into" it, and the parsed files also didn't match the name x.html. So attempting to execute it results in an error.

Now if we try to execute one of its associated, named template:

if err := t.ExecuteTemplate(os.Stdout, "a.html", nil); err != nil {
    panic(err)
}

It succeeds, and again gives:

I'm a.

这篇关于Go Template ParseFiles函数可解析多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆