Template Inheritance porblem

xuanbao · · 615 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, I am passing data from controller to template &#34;index.html&#34; to render them.</p> <p>index.html file includes from sidebar.html, so index.html file is made up of sidebar.html too</p> <p>The problem is that, if I try to access data inside sidebar.html, it is empty.</p> <p>What is the solution?</p> <hr/>**评论:**<br/><br/>tgaz: <pre><p><a href="https://golang.org/pkg/text/template/:" rel="nofollow">https://golang.org/pkg/text/template/:</a></p> <pre><code>{{template &#34;name&#34;}} The template with the specified name is executed with nil data. {{template &#34;name&#34; pipeline}} The template with the specified name is executed with dot set to the value of the pipeline. </code></pre> <p>You should be using the second form, and likely pass dot as the pipeline.</p></pre>ComfortablyNull: <pre><p>Are you using the <a href="https://golang.org/pkg/html/template/" rel="nofollow">template</a> package? </p> <p>If so you probably have to pass the data to the sidebar template with &#39;.&#39; :</p> <p>Quick and dirty example:</p> <pre><code>package main import ( &#34;html/template&#34; &#34;os&#34; ) // Notice the &#39;.&#39; being passed after the template name. // This passes all the curently scoped data to the template const index = `{{ .hello }} {{template &#34;sidebar&#34; .}}` const sidebar = `{{ .bye }}` func main() { data := map[string]string{&#34;hello&#34;: &#34;hello&#34;, &#34;bye&#34;: &#34;goodbye!&#34;} template.ParseFiles() sideBarTemplate, err := template.New(&#34;sidebar&#34;).Parse(sidebar) if err != nil { panic(err) } t, err := sideBarTemplate.New(&#34;index&#34;).Parse(index) if err != nil { panic(err) } t.Execute(os.Stdout, data) } </code></pre> <p>This should output:</p> <blockquote> <p>hello goodbye!</p> </blockquote></pre>CrappyFap69: <pre><p>It works like a charm!</p></pre>LaughingCabbage_: <pre><p><code> &lt;html&gt; &lt;body&gt;{{ .class.member }}&lt;/body&gt; &lt;/html&gt; </code></p></pre>

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

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