<p>I have problem with go, martini and how martini works with templates.</p>
<pre><code>package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
type Page struct {
Caption string
Body string
Tmpl string
}
func main() {
webApp = martini.Classic()
webApp.Get("/", indexHandler)
webApp.Use(render.Renderer(render.Options{
Directory: "templates",
Layout: "layout",
Extensions: []string{".tmpl", ".html"},
Delims: render.Delims{"{{", "}}"},
Charset: "UTF-8",
IndentJSON: true}))
webApp.Run()
}
func indexHandler(r render.Render) {
var indexPage Page
indexPage.Caption = "index"
indexPage.Tmpl = "subIndex"
r.HTML(http.StatusOK, "index", indexPage)
}
</code></pre>
<p>subIndex.tmpl is simple
<h3>subindex</h3></p>
<p>If I use in index.tmpl with fallowing code:</p>
<pre><code><h2>Caption{{.Caption}}</h2>
{{ .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><h2>Caption{{.Caption}}</h2>
{{ template .Tmpl }}
I get error unexpected "Tmpl" in template invocation Same error message
<h2>Caption{{.Caption}}</h2>
{{ $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'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传