英文:
html/template: "layout" is undefined
问题
我尝试使用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
。
英文:
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
我的问题是我从随机目录中运行了go
文件。为了解决这个问题,我将目录(cd
)更改为templates
文件夹的上级目录。
英文:
My problem was that I run go
file from random directory. To solve it I changed directory (cd
) to parent of templates
folder.
答案2
得分: -1
在</body>
之后添加正确的html
闭合标签
就是这样:
英文:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论