Using "with" in template

agolangf · · 621 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I did this with some old code and now, with a new template, I can&#39;t get the same thing to work so I guess I&#39;m forgetting something. This is just sloppy learning code below.</p> <p>In my handler, I have:</p> <pre><code>var page = Page { Title: &#34;My Title&#34;, } var homepage = template.Must(template.ParseFiles( &#34;../templates/base.tmpl&#34;, &#34;../templates/head.tmpl&#34;, &#34;../templates/main-nav.html&#34;, &#34;../templates/homepage.tmpl&#34;, &#34;../templates/footer.html&#34;, )) homepage.ExecuteTemplate(w,&#34;base&#34;,&amp;page) </code></pre> <p>In my template I did:</p> <pre><code>{{define &#34;head&#34;}} {{with .page}}&lt;title&gt;{{.Title}}&lt;/title&gt;{{end}} {{end}} </code></pre> <p>along with a few variations. I&#39;m trying to get the title to show but not even &lt;title&gt; appears in the page. I did try it with just {{.Title}}, too, and then the &lt;title&gt; element appears. Other elements that I don&#39;t show for &#34;head&#34; above do appear.</p> <p>What am I forgetting?</p> <p>EDIT: I&#39;m wondering, now, if my problem arises from trying to pass a structure to a templated nested among other templates. iow, the &#34;head&#34; template is nested within the &#34;base&#34; template. I thought structures passed with Execute Template would still get there. Am I wrong?</p> <p>EDIT2: <strong>Bingo!</strong> I put the title in the base template and it shows up. So I need to investigate further to see how to get that struct into one of the nested templates.</p> <hr/>**评论:**<br/><br/>dhdfdh: <pre><p>And the solution is: In order to pass structs to nested templates, you have to set up context for that template. Easy enough to do:</p> <pre><code>{{template &#34;head&#34; .}} </code></pre> <p>Notice the dot. I&#39;ve seen example code with and without the dot and wasn&#39;t sure of the difference. I have to do more reading about this but that solved the problem. It all relates to pipelining.</p></pre>casba: <pre><p>Wouldn&#39;t .page attempt to reference a field (non exported, but anyways) in the provided structure? &#34;.&#34; In this context is your page struct, you shouldn&#39;t attempt to reference it.</p></pre>dhdfdh: <pre><p>If you mean do this:</p> <pre><code>{{with .page}}&lt;title&gt;{{.}}&lt;/title&gt;{{end}} </code></pre> <p>then, yes, I tried that to no avail.</p></pre>casba: <pre><p>No that&#39;s not what I mean. page is the struct you pass in, which means &#34;.&#34; <em>is</em> page. Doing .page is equiv to page.page. You probably want something like </p> <p>{{with .Title}} &lt;title&gt; {{.}} &lt;/title&gt; {{end-}}</p> <p>Sorry for formatting, on phone.</p></pre>dhdfdh: <pre><p>Thanks for reminding me about that possibility but, no, it didn&#39;t change anything.</p></pre>casba: <pre><p>Hm that&#39;s odd, here&#39;s a play example that I think demonstrates what I&#39;m talking about, maybe your example differs slightly. <a href="http://play.golang.org/p/C7bhkAd0C6">http://play.golang.org/p/C7bhkAd0C6</a></p></pre>dhdfdh: <pre><p>That works. There must be a mistake in how I&#39;m passing the structure and I just don&#39;t see it or I forgot something.</p></pre>arschles: <pre><p>Does it work if you pass this in?</p> <pre><code>var data = struct{Page Page}{ Page: Page{Title:&#34;My Title&#34;}, } </code></pre></pre>dhdfdh: <pre><p>No, it doesn&#39;t.</p></pre>arschles: <pre><p>ohhh, the title. totally missed that!</p></pre>hobbified: <pre><p>You&#39;re referencing <code>.page</code>, there is no thing called <code>.page</code>. You didn&#39;t pass your template a map with a <code>&#34;page&#34;</code> key, or a struct with a <code>page</code> field (which you couldn&#39;t use anyway since it would be private), so <code>.page</code> is completely wrong. You passed the Page object to the template itself, so the Page object is simply <code>.</code> and the title is <code>.Title</code>.</p></pre>dhdfdh: <pre><p>Not only is your answer wrong, it was supplied by someone else hours ago and is the longest thread here. On top of that, I supplied the solution to the problem myself in the answers.</p></pre>: <pre><p>[deleted]</p></pre>dhdfdh: <pre><p>Uh. Of course I was wrong and made a mistake. That&#39;s why I asked the question. I even said both things in my post and elsewhere. You&#39;re hours late to the party, didn&#39;t even read anything, the solution was already given, you get called on it and now you&#39;re saying I&#39;m the problem?</p></pre>: <pre><p>[deleted]</p></pre>dhdfdh: <pre><blockquote> <p>as long as you keep ignoring correct advice you&#39;re never going to fix your bug.</p> </blockquote> <p>The bug was fixed 7 hours before you posted your solution which was already stated as not fixing the problem 10 hours ago.</p> <blockquote> <p>as long as you treat everyone you work with like an idiot you&#39;re never going to be a remotely competent programmer.</p> </blockquote> <p>In 1992, when I sat next to Jim Clark in the lunch room at SGI, we were having a conversation when he said, &#34;You&#39;re a pretty good programmer!&#34; </p> <blockquote> <p>you condescendingly tell someone trying to help you that they&#39;re wrong</p> </blockquote> <p>You were wrong cause you offered the same solution someone else offered and it didn&#39;t fix the problem. That was 10 hours before you posted yours.</p> <blockquote> <p>Admit to yourself that the code you provided contains nothing that would make ...</p> </blockquote> <p>I&#39;ll repeat myself: of course my supplied code wouldn&#39;t work and I said so! </p> <p>Now, by not reading or understanding anything I said directly to you, and not reading or understanding <strong>any</strong> of the previous posts in this thread, you&#39;re making a total ass of yourself.</p></pre>

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

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