Trouble rendering templates in Go

xuanbao · · 413 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, I&#39;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&#39;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&#39;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(&#34;views/user/*&#34;) if err != nil { log.Fatal(err) } layouts, err := filepath.Glob(&#34;views/layouts/*&#34;) 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&#39;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

413 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传