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

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
  • 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)? – David Budworth Dec 14 '14 at 13:03

2 Answers2

2

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

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
-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>
bikbah
  • 361
  • 2
  • 5