Golang Template range of struct in struct

polaris · · 465 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Comment with better formatting on: <a href="https://pastebin.com/DnvnNzSY" rel="nofollow">https://pastebin.com/DnvnNzSY</a></p> <p>Hi i try to write in an template the Title and Text via an range function. But it dint work - i cant find a solution in the net:</p> <p>Struct Model:<br/> type Article struct {<br/> Articletitle []template.HTML<br/> ArticleText []template.HTML<br/> } </p> <p>type lista struct { Login bool Articletitle []template.HTML LoginText string Inventarlist []string ArticleText []template.HTML Article Article }</p> <p>Template: this is working:</p> <p>{{range $.Article.ArticleText}} &lt;article&gt; {{.}}...&lt;br&gt; &lt;/article&gt; &lt;hr&gt; {{end}}</p> <p>This not:</p> <p>{{range $.Article}} &lt;article&gt; &lt;h1&gt;{{.Articletitle}}&lt;/h1&gt; {{$.ArticleText}}...&lt;br&gt; &lt;/article&gt; &lt;hr&gt; {{end}}</p> <hr/>**评论:**<br/><br/>VerilyAMonkey: <pre><p>.Article is a struct, not a list or a map. You can&#39;t range over it.</p> <p>Instead of just one Article containing a list of titles and texts, you should make a list of Articles where each one contains only the title and text for a single article. You can range over that list of Articles. </p></pre>hakimoto: <pre><p>Can you please give me a working example of the template syntax in this context?</p></pre>VerilyAMonkey: <pre><p><a href="https://play.golang.org/p/5Z2moskNGE" rel="nofollow">https://play.golang.org/p/5Z2moskNGE</a></p></pre>

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

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