2

I try to use martini framework with layout template:

package main

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

func main() {
    m := martini.Classic()

    m.Use(render.Renderer(render.Options{Directory: "./templates",
        Layout:     "layout",
        Extensions: []string{".tmpl"},
    }))

    m.Get("/", func(r render.Render) {
        r.HTML(200, "mainPage", map[string]interface{}{"Title": "some title", "H1": "some h1"})
    })

    m.Run()
}

In the same folder as this main.go file I got folder templates with layout.tmpl file:

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

and mainPage.tmpl file:

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

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

When I open http://localhost:3000/ in browser I see error: html/template: "layout" is undefined

1
  • 2
    Haven't used martini, but it is a common issue to have templates fail at runtime due to you not being in the right directory when starting your app, have you tried to run from your main folder (parent of templates)? Commented Dec 14, 2014 at 13:03

2 Answers 2

2

My problem was that I run go file from random directory. To solve it I changed directory (cd) to parent of templates folder.

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

Make correct html close tag after </body>

That`s it:

<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>
1
  • That typo didn't affect my error “layout” is undefined Commented Dec 14, 2014 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.