【发布时间】:2014-12-14 10:12:54
【问题描述】:
我尝试使用带有布局模板的 martini 框架:
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()
}
在与此 main.go 文件相同的文件夹中,我得到文件夹 templates 和 layout.tmpl 文件:
<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>
和mainPage.tmpl 文件:
{{define "left"}}
left content
{{end}}
{{define "right"}}
right content
{{end}}
当我在浏览器中打开 http://localhost:3000/ 时,我看到错误:
html/template: "layout" is undefined
【问题讨论】:
-
没有使用过martini,但是由于您在启动应用程序时不在正确的目录中而导致模板在运行时失败是一个常见问题,您是否尝试过从主文件夹运行(模板的父级)?