<p>Is it possible to have dynamic nested templates in Go? For example, let'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 <- Depending on if a user is logged in this renders a different version of this template, let'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 "sidebar_auth.html" }}
{{ else }}
{{ template "sidebar.html" }}
{{ 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'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
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传