html template with martini in golang

agolangf · · 951 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have problem with go, martini and how martini works with templates.</p> <pre><code>package main import ( &#34;github.com/go-martini/martini&#34; &#34;github.com/martini-contrib/render&#34; ) type Page struct { Caption string Body string Tmpl string } func main() { webApp = martini.Classic() webApp.Get(&#34;/&#34;, indexHandler) webApp.Use(render.Renderer(render.Options{ Directory: &#34;templates&#34;, Layout: &#34;layout&#34;, Extensions: []string{&#34;.tmpl&#34;, &#34;.html&#34;}, Delims: render.Delims{&#34;{{&#34;, &#34;}}&#34;}, Charset: &#34;UTF-8&#34;, IndentJSON: true})) webApp.Run() } func indexHandler(r render.Render) { var indexPage Page indexPage.Caption = &#34;index&#34; indexPage.Tmpl = &#34;subIndex&#34; r.HTML(http.StatusOK, &#34;index&#34;, indexPage) } </code></pre> <p>subIndex.tmpl is simple &lt;h3&gt;subindex&lt;/h3&gt;</p> <p>If I use in index.tmpl with fallowing code:</p> <pre><code>&lt;h2&gt;Caption{{.Caption}}&lt;/h2&gt; {{ .Tmpl }} // caption works, .Tmpl works and see subIndex as string ) </code></pre> <p>If I use in index.tmpl with fallowing code:</p> <pre><code>&lt;h2&gt;Caption{{.Caption}}&lt;/h2&gt; {{ template .Tmpl }} I get error unexpected &#34;Tmpl&#34; in template invocation Same error message &lt;h2&gt;Caption{{.Caption}}&lt;/h2&gt; {{ $templateName := .Tmpl }} {{ $templateName }} {{ template $templateName }} </code></pre> <p>I have built program with go build, and stared it, when I open browsers with localhost:3000, i got errors which I have mentioned. I want to pass to index template which template to load in this case it&#39;s subindex.tmpl.</p> <p>What am I missing?</p> <hr/>**评论:**<br/><br/>1986_1979: <pre><p>indexPage.Tmpl property may need to be the name of the template with the correct extension , subIndex.tmpl .</p> <p><a href="http://gohugo.io/templates/go-templates/" rel="nofollow">http://gohugo.io/templates/go-templates/</a></p></pre>

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

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