Passing data to a html template

polaris · · 553 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m thinking out loud but my initial attempt at this failed. I hope I explain what I&#39;m trying to do good enough but, as I typed this, I realize it might be confusing.</p> <p>In template.ExecuteTemplate, I pass &#34;person&#34; which is equal to the map People[&#34;Joe Blow&#34;] which is a collection of data on that one person like name, address, etc. and it works. &#34;People&#34;, of course, is a collection of people.</p> <p>Now I have a different template where I want People[&#34;Joe Blow&#34;] at the top of the page followed by a list of all &#34;People&#34;. I tried passing to template.ExecuteTemplate a struct containing both &#34;person&#34; and &#34;People&#34; but get an error complaining about no such field as &#34;Person&#34;. </p> <p>iow, I want to display all the data in People[&#34;Joe Blow&#34;] followed by the list of all &#34;People&#34; and I haven&#39;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&#39;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&#39;m doing but, so far, I don&#39;t see how it helps with passing in the struct data.</p></pre>elithrar_: <pre><p>Post the code you have—even if it&#39;s just pseudocode. You&#39;ll either want a <code>{{ range .People }}</code> or similar—but I&#39;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&#39;m missing. </p> <p>What I have is relatively complex. I&#39;m having trouble expressing it which is why I haven&#39;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&#39;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&gt;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 &#39;range&#39; 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&#39;m feeling the effects of not doing so.</p> <p>I&#39;ll test when I get home. Thanks!</p></pre>

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

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