我嘗試將馬提尼框架與布局模板一起使用:package mainimport ( "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}}當(dāng)我http://localhost:3000/在瀏覽器中打開時,我看到錯誤: html/template: "layout" is undefined
2 回答

慕哥6287543
TA貢獻(xiàn)1831條經(jīng)驗 獲得超10個贊
我的問題是我go
從隨機(jī)目錄運(yùn)行文件。為了解決它,我將目錄 ( cd
)更改為templates
文件夾的父級。
- 2 回答
- 0 關(guān)注
- 174 瀏覽
添加回答
舉報
0/150
提交
取消