<p>I'm thinking out loud but my initial attempt at this failed. I hope I explain what I'm trying to do good enough but, as I typed this, I realize it might be confusing.</p>
<p>In template.ExecuteTemplate, I pass "person" which is equal to the map People["Joe Blow"] which is a collection of data on that one person like name, address, etc. and it works. "People", of course, is a collection of people.</p>
<p>Now I have a different template where I want People["Joe Blow"] at the top of the page followed by a list of all "People". I tried passing to template.ExecuteTemplate a struct containing both "person" and "People" but get an error complaining about no such field as "Person". </p>
<p>iow, I want to display all the data in People["Joe Blow"] followed by the list of all "People" and I haven't quite figured it out if someone knows what I mean and can get me started.</p>
<hr/>**评论:**<br/><br/>elithrar_: <pre><p><a href="https://jan.newmarch.name/go/template/chapter-template.html" rel="nofollow">https://jan.newmarch.name/go/template/chapter-template.html</a></p></pre>dhdfdh: <pre><p>I'm beyond knowing how to make a template, since I have a nearly complete, small web site with extensive use of templates. </p>
<p>EDIT: Actually, that link, which I have looked at quite often over the past couple of months, is somewhat similar to what I'm doing but, so far, I don't see how it helps with passing in the struct data.</p></pre>elithrar_: <pre><p>Post the code you have—even if it's just pseudocode. You'll either want a <code>{{ range .People }}</code> or similar—but I'm only guessing.</p>
<p>Even just knowing how your structs/maps are nested will help.</p></pre>dhdfdh: <pre><p>Yes. I think range is all I'm missing. </p>
<p>What I have is relatively complex. I'm having trouble expressing it which is why I haven't supplied anything. Tried three times but deleted it.</p></pre>Spirit_of_Stallman: <pre><p>Give url with a code, which placed on <a href="http://play.golang.org/" rel="nofollow">play</a>.</p></pre>dhdfdh: <pre><p>As I stated, there is no code other than a sloppy failed attempt which I know is the wrong way, and I'm looking for an idea on where to start.</p></pre>cryp7ix: <pre><p>Disagreed, your syntax might just be slightly off - nobody can tell without the code.</p>
<p>I usually use an anonymous struct type as an entrypoint to list all the name>type fields I want to use in my template. <a href="https://play.golang.org/p/Xzc90MyXya" rel="nofollow">example</a>. </p>
<p><a href="https://play.golang.org/p/rpdKmfJ36W" rel="nofollow">example2 - with slice</a></p></pre>dhdfdh: <pre><p>Your second example is similar to my first quick run through but, as I said in a different post, I forgot to put 'range' in. </p>
<p>I needed to take a drive through the country and walk the dogs to clear my head; something I usually do on a Sunday and I'm feeling the effects of not doing so.</p>
<p>I'll test when I get home. Thanks!</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传