在golang中使用template.ParseFiles的多个文件
发布时间:2020-12-16 19:20:03 所属栏目:大数据 来源:网络整理
导读:例如,我有 package mainimport "html/template"import "net/http"func handler(w http.ResponseWriter,r *http.Request) { t,_ := template.ParseFiles("header.html","footer.html") t.Execute(w,map[string] string {"Title": "My title","Body": "Hi thi
例如,我有
package main import "html/template" import "net/http" func handler(w http.ResponseWriter,r *http.Request) { t,_ := template.ParseFiles("header.html","footer.html") t.Execute(w,map[string] string {"Title": "My title","Body": "Hi this is my body"}) } func main() { http.HandleFunc("/",handler) http.ListenAndServe(":8080",nil) } 在header.html中: Title is {{.Title}} 在footer.html中: Body is {{.Body}} 当访问http:// localhost:8080 /,我只看到“标题是我的标题”,而不是第二个文件footer.html。如何使用template.ParseFiles加载多个文件?最有效的方法是什么? 提前致谢。
只有第一个文件被用作主模板。其他模板文件需要从头开始包含:
Title is {{.Title}} {{template "footer.html" .}} “footer.html”后的点将数据从执行通过传递到页脚模板 – 传递的值将变为。在包含的模板中。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |