Maxim Yefremov
Maxim Yefremov

Reputation: 14185

panic: template: redefinition of template

I got layout.tmpl:

<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>  
    <div id='left'>
        {{template "left" .}}
    </div>
    <div id='right'>
        {{template "right" .}}
    </div>
</body>
</html>

and mainPage.tmpl:

{{define "left"}}
  left content
{{end}}

{{define "right"}}
    right content
{{end}}

and someOtherPage.tmpl:

{{define "left"}}
  left content 2
{{end}}

{{define "right"}}
    right content 2
{{end}}

and martini go web app using that templates martiniWebApp.go:

package main
import (
    "github.com/go-martini/martini"
    "github.com/martini-contrib/render"
)

func main() {
    m := martini.Classic()
    m.Use(render.Renderer(render.Options{
        Layout: "layout",
    }))

    m.Get("/", func(r render.Render) {
        r.HTML(200, "mainPage", nil)
    })

    m.Get("/somePage", func(r render.Render) {
        r.HTML(200, "someOtherPage", nil)
    })
    m.Run()
}

When I run my app go run martiniWebApp.go I got error:

panic: template: redefinition of template "left"

If I remove file someOtherPage.tmpl and route /somePage from web app then error disappear. But how to organise layout-block construction to resuse common layout html and define only few blocks on every specific page?

Upvotes: 1

Views: 337

Answers (1)

LenW
LenW

Reputation: 3096

You can go the other way around and and include the pieces you want in the page. Something like

{{template "header.html" .}}

 contents 

{{template "footer.html" .}}

Upvotes: 1

Related Questions