1

I have problem with go, martini and how martini works with templates.

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)
}

If I use in index.tmpl with fallowing code:

 <h2>Caption{{.Caption}}</h2>
{{ .Tmpl }}

// caption works, .Tmpl works and see subIndex as string )

If I use in index.tmpl with fallowing 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 }}

subIndex.tmpl is simple

 <h3>subindex</h3>

I have built program with go build, and stared it, when I open browsers with localhost:3000, i got errors which I have mentioned.

What am I missing?

insomniac669
  • 57
  • 1
  • 7

0 Answers0