dpmrakfbx820320638 2017-07-07 12:39
浏览 213
已采纳

Go Template ParseFiles函数可解析多个文件

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?

  • 写回答

1条回答 默认 最新

  • dpf56454 2017-07-07 13:01
    关注

    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.
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 访问url时不会自动调用其 Servlet的doGet()
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部