关于go的模板问题

2013-11-26 10:19:29 +08:00
 xdeng
h.html
<p>{{.H}}</p>


c.html
{{template "h.html"}}
<p>{{.C}}</p>
{{template "f.html"}}


f.html
<p>{{.F}}</p>


type data_t struct {
H string
C string
F string
}


t := template.ParseFiles("h.html", "c.html", "f.html") //加载三个

index := data_t{H: "头", C: "内容", F: "尾"}

t.ExecuteTemplate(rw, "c.html", index)
为什么只替换了C , H和F没替换, c.html不是包含了 f.html和h.html吗?按理说也会自动替换啊。




把c.html改成
c.html
<p>{{.C}}</p>

然后
t.ExecuteTemplate(rw, "h.html", index)
t.ExecuteTemplate(rw, "c.html", index)
t.ExecuteTemplate(rw, "f.html", index)
这样连着就可以, 这样未免也太挫了, 那上面的包含还有意义吗? 请教有没有更好的办法。。。
1223 次点击
所在节点    Go 编程语言
6 条回复
xdeng
2013-11-26 10:23:39 +08:00
@wwwjfy
@ jun0205 能帮忙么
xdeng
2013-11-26 10:23:51 +08:00
@jun0205 能帮忙么
gihnius
2013-11-26 10:35:46 +08:00
c.html
{{template "h.html"}}
<p>{{.C}}</p>
{{template "f.html"}}

改成

c.html
{{template "h.html" .}}
<p>{{.C}}</p>
{{template "f.html" .}}

才能将数据传过去
xdeng
2013-11-26 10:49:21 +08:00
@gihnius 非常感谢! 还真是这样。。。 我找的go资料很少
xdeng
2013-11-26 10:50:16 +08:00
@gihnius 你能有 好的 全面一点的 资料分享一下么! 官方的太不全了
sillyousu
2013-11-30 13:11:44 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/90638

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX