How to properly do templating with multiple templates and multiple pipelines

agolangf · · 394 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>The usual examples of the template package I see is a struct with 2 simple strings which get called through {{.First}} {{.Last}} in html. Really obvious stuff.</p> <p>Now what do I do if I have a complex template? Let&#39;s assume I have a table in my index.html. I already splitted that table out as a new template with {{define &#34;entries&#34;}} {{range .}} {{.Name}} {{.Value}} {{end}}{{end}}</p> <p>In my code I have a slice of a custom struct (with Name and Value as fields) which gets filled during operation and then the template gets executed. Everything works (albeit it is terribly slow since the operation has to finish before the template gets executed if theres another way I&#39;d love to hear). </p> <p>Problem is now what do I do if I want to use additional templates in the same file? I cant just create a new struct since I use the original struct for my table body. If I try to nest the Table Body struct in a new struct I cant access its data anymore. Also if I have more than 2 Templates only 1 gets the piped data appearently. Maybe Im doing something wrong here? </p> <hr/>**评论:**<br/><br/>Spirit_of_Stallman: <pre><p>I.e. you want to reuse your table data in several points of a template? If I have correctly understood a question, then here a small <a href="https://play.golang.org/p/_JkNKLJDE8" rel="nofollow">example</a> for you.</p></pre>fawa123: <pre><p>I rather mean how to pass multiple data sets to multiple templatrs then rendering Thema in one base template</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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