Dynamic nested templates?

blov · · 311 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Is it possible to have dynamic nested templates in Go? For example, let&#39;s say I have a page called index with a header, footer, and sidebar. I want the sidebar to be a completely different template if a user is logged in.</p> <pre><code>header.html index.html sidebar.html &lt;- Depending on if a user is logged in this renders a different version of this template, let&#39;s call it sidebar.html or sidebar_auth.html. footer.html </code></pre> <p>Currently I am parsing all my templates at the launch of my app.</p> <hr/>**评论:**<br/><br/>Spirit_of_Stallman: <pre><pre><code>{{ if .LoggedIn }} {{ template &#34;sidebar_auth.html&#34; }} {{ else }} {{ template &#34;sidebar.html&#34; }} {{ end }} </code></pre> <p>And send dat <code>LoggedIn</code> on template execute?</p></pre>NikkoTheGreeko: <pre><p>That looks like it should work. I&#39;ll try it, thank you.</p></pre>tipsqueal: <pre><p>Yes you can and you can pass data to the templates as well. See this stack overflow post:</p> <p><a href="http://stackoverflow.com/questions/17206467/go-how-to-render-multiple-templates-in-golang" rel="nofollow">http://stackoverflow.com/questions/17206467/go-how-to-render-multiple-templates-in-golang</a></p></pre>

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

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