douba1498 2015-03-14 05:33
浏览 51
已采纳

如何在golang的模板中多次使用解析

i need help. Now to output pages I use multiple templates(1 example) and i want to use parse many times in one template(2 example)

Example 1:

...
t, err := template.ParseFiles("header.html")
t.Execute(wr, data)
d, err := template.ParseFiles("content.html")
d.Execute(wr, datatwo)
...

Example 2:

...
t := template.New("blabla")
t.ParseFiles("header.html")
t.ParseFiles("content.html")
t.Execute("wr", data)

P.S. I'm sorry, my english is very bad

  • 写回答

1条回答 默认 最新

  • doufei1893 2015-03-14 06:17
    关注

    template.ParseFiles can take multiple filenames as an argument, as per http://golang.org/pkg/html/template/#ParseFiles

    func ParseFiles(filenames ...string) (*Template, error)

    e.g.

    t := template.New("base")
    t, err := t.ParseFiles("header.html", "footer.html", "content.html")
    if err != nil {
        // handle the error
    }
    

    There's a solid example on how to use html/template in the Go docs as well: http://golang.org/doc/articles/wiki/#tmp_6

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

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了