<p>I did this with some old code and now, with a new template, I can't get the same thing to work so I guess I'm forgetting something. This is just sloppy learning code below.</p>
<p>In my handler, I have:</p>
<pre><code>var page = Page {
Title: "My Title",
}
var homepage = template.Must(template.ParseFiles(
"../templates/base.tmpl",
"../templates/head.tmpl",
"../templates/main-nav.html",
"../templates/homepage.tmpl",
"../templates/footer.html",
))
homepage.ExecuteTemplate(w,"base",&page)
</code></pre>
<p>In my template I did:</p>
<pre><code>{{define "head"}}
{{with .page}}<title>{{.Title}}</title>{{end}}
{{end}}
</code></pre>
<p>along with a few variations. I'm trying to get the title to show but not even <title> appears in the page. I did try it with just {{.Title}}, too, and then the <title> element appears. Other elements that I don't show for "head" above do appear.</p>
<p>What am I forgetting?</p>
<p>EDIT: I'm wondering, now, if my problem arises from trying to pass a structure to a templated nested among other templates. iow, the "head" template is nested within the "base" 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 "head" .}}
</code></pre>
<p>Notice the dot. I've seen example code with and without the dot and wasn'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't .page attempt to reference a field (non exported, but anyways) in the provided structure? "." In this context is your page struct, you shouldn't attempt to reference it.</p></pre>dhdfdh: <pre><p>If you mean do this:</p>
<pre><code>{{with .page}}<title>{{.}}</title>{{end}}
</code></pre>
<p>then, yes, I tried that to no avail.</p></pre>casba: <pre><p>No that's not what I mean. page is the struct you pass in, which means "." <em>is</em> page. Doing .page is equiv to page.page. You probably want something like </p>
<p>{{with .Title}} <title> {{.}} </title> {{end-}}</p>
<p>Sorry for formatting, on phone.</p></pre>dhdfdh: <pre><p>Thanks for reminding me about that possibility but, no, it didn't change anything.</p></pre>casba: <pre><p>Hm that's odd, here's a play example that I think demonstrates what I'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'm passing the structure and I just don'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:"My Title"},
}
</code></pre></pre>dhdfdh: <pre><p>No, it doesn't.</p></pre>arschles: <pre><p>ohhh, the title. totally missed that!</p></pre>hobbified: <pre><p>You're referencing <code>.page</code>, there is no thing called <code>.page</code>. You didn't pass your template a map with a <code>"page"</code> key, or a struct with a <code>page</code> field (which you couldn'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's why I asked the question. I even said both things in my post and elsewhere. You're hours late to the party, didn't even read anything, the solution was already given, you get called on it and now you're saying I'm the problem?</p></pre>: <pre><p>[deleted]</p></pre>dhdfdh: <pre><blockquote>
<p>as long as you keep ignoring correct advice you'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'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, "You're a pretty good programmer!" </p>
<blockquote>
<p>you condescendingly tell someone trying to help you that they're wrong</p>
</blockquote>
<p>You were wrong cause you offered the same solution someone else offered and it didn'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'll repeat myself: of course my supplied code wouldn'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're making a total ass of yourself.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传