<p>Hi, I'm having trouble rendering templates in Go. I have the question explained in stackoverflow: <a href="http://stackoverflow.com/questions/36710562/rendering-template-in-golang">http://stackoverflow.com/questions/36710562/rendering-template-in-golang</a>
Can somebody please tell me what I am doing wrong here. I'm having some trouble familiarising templates. </p>
<p>PS: Sorry if linking to stackoverflow is not along the community norm. Let me know and I'll gladly post the entire question here.</p>
<hr/>**评论:**<br/><br/>p765: <pre><p>Managed to solve this. This page <a href="https://elithrar.github.io/article/approximating-html-template-inheritance/" rel="nofollow">https://elithrar.github.io/article/approximating-html-template-inheritance/</a> helped.</p>
<p>Basically, I had to change the code with which I parsed the templates to:</p>
<pre><code>tpls, err := filepath.Glob("views/user/*")
if err != nil {
log.Fatal(err)
}
layouts, err := filepath.Glob("views/layouts/*")
if err != nil {
log.Fatal(err)
}
for _, layout := range layouts {
files := append(layouts, tpls)
t.templates = template.Must(template.ParseFiles(files...))
}
</code></pre></pre>bestform: <pre><p>thanks for posting the solution you've found. Even including the source. Good job. :)</p></pre>p765: <pre><p>Thanks. Btw, I also had to change the t.templates to a string map so that I can have multiple templates in it.
Like so:</p>
<pre><code>for ...
...
t.templates[name] = template.Must(template.ParseFiles(files...))
</code></pre></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传