html/template: “layout”未定义

huangapple go评论121阅读模式
英文:

html/template: "layout" is undefined

问题

我尝试使用martini框架和布局模板:

  1. package main
  2. import (
  3. "github.com/go-martini/martini"
  4. "github.com/martini-contrib/render"
  5. )
  6. func main() {
  7. m := martini.Classic()
  8. m.Use(render.Renderer(render.Options{Directory: "./templates",
  9. Layout: "layout",
  10. Extensions: []string{".tmpl"},
  11. }))
  12. m.Get("/", func(r render.Render) {
  13. r.HTML(200, "mainPage", map[string]interface{}{"Title": "some title", "H1": "some h1"})
  14. })
  15. m.Run()
  16. }

在与main.go文件相同的文件夹中,我有一个名为templates的文件夹,里面有一个layout.tmpl文件:

  1. <html xmlns="http://www.w3.org/1999/xhtml"></html>
  2. <head></head>
  3. <body>
  4. <div id='left'>
  5. {{template "left" .}}
  6. </div>
  7. <div id='right'>
  8. {{template "right" .}}
  9. </div>
  10. </body>

还有一个mainPage.tmpl文件:

  1. {{define "left"}}
  2. left content
  3. {{end}}
  4. {{define "right"}}
  5. right content
  6. {{end}}

当我在浏览器中打开http://localhost:3000/时,我看到错误消息:html/template: "layout" is undefined

英文:

I try to use martini framework with layout template:

  1. package main
  2. import (
  3. &quot;github.com/go-martini/martini&quot;
  4. &quot;github.com/martini-contrib/render&quot;
  5. )
  6. func main() {
  7. m := martini.Classic()
  8. m.Use(render.Renderer(render.Options{Directory: &quot;./templates&quot;,
  9. Layout: &quot;layout&quot;,
  10. Extensions: []string{&quot;.tmpl&quot;},
  11. }))
  12. m.Get(&quot;/&quot;, func(r render.Render) {
  13. r.HTML(200, &quot;mainPage&quot;, map[string]interface{}{&quot;Title&quot;: &quot;some title&quot;, &quot;H1&quot;: &quot;some h1&quot;})
  14. })
  15. m.Run()
  16. }

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

  1. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;/html&gt;
  2. &lt;head&gt;&lt;/head&gt;
  3. &lt;body&gt;
  4. &lt;div id=&#39;left&#39;&gt;
  5. {{template &quot;left&quot; .}}
  6. &lt;/div&gt;
  7. &lt;div id=&#39;right&#39;&gt;
  8. {{template &quot;right&quot; .}}
  9. &lt;/div&gt;
  10. &lt;/body&gt;

and mainPage.tmpl file:

  1. {{define &quot;left&quot;}}
  2. left content
  3. {{end}}
  4. {{define &quot;right&quot;}}
  5. right content
  6. {{end}}

When I open http://localhost:3000/ in browser I see error:
html/template: &quot;layout&quot; 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



{{template "left" .}}

&lt;/body&gt;之后添加正确的html闭合标签

就是这样:



{{template "left" .}}


英文:
  1. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;/html&gt;
  2. &lt;head&gt;&lt;/head&gt;
  3. &lt;body&gt;
  4. &lt;div id=&#39;left&#39;&gt;
  5. {{template &quot;left&quot; .}}
  6. &lt;/div&gt;
  7. &lt;div id=&#39;right&#39;&gt;
  8. {{template &quot;right&quot; .}}
  9. &lt;/div&gt;
  10. &lt;/body&gt;

Make correct html close tag after &lt;/body&gt;

That`s it:

  1. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  2. &lt;head&gt;&lt;/head&gt;
  3. &lt;body&gt;
  4. &lt;div id=&#39;left&#39;&gt;
  5. {{template &quot;left&quot; .}}
  6. &lt;/div&gt;
  7. &lt;div id=&#39;right&#39;&gt;
  8. {{template &quot;right&quot; .}}
  9. &lt;/div&gt;
  10. &lt;/body&gt;
  11. &lt;/html&gt;

huangapple
  • 本文由 发表于 2014年12月14日 18:12:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/27468281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定