html/template: “layout”未定义

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

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 (
	&quot;github.com/go-martini/martini&quot;
	&quot;github.com/martini-contrib/render&quot;
)

func main() {
	m := martini.Classic()

	m.Use(render.Renderer(render.Options{Directory: &quot;./templates&quot;,
		Layout:     &quot;layout&quot;,
		Extensions: []string{&quot;.tmpl&quot;},
	}))

	m.Get(&quot;/&quot;, func(r render.Render) {
		r.HTML(200, &quot;mainPage&quot;, map[string]interface{}{&quot;Title&quot;: &quot;some title&quot;, &quot;H1&quot;: &quot;some h1&quot;})
	})

	m.Run()
}

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

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;/html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;	
	&lt;div id=&#39;left&#39;&gt;
		{{template &quot;left&quot; .}}
	&lt;/div&gt;
	&lt;div id=&#39;right&#39;&gt;
		{{template &quot;right&quot; .}}
	&lt;/div&gt;
&lt;/body&gt;

and mainPage.tmpl file:

{{define &quot;left&quot;}}
  left content
{{end}}

{{define &quot;right&quot;}}
	right content
{{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" .}}


英文:
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;/html&gt;
   &lt;head&gt;&lt;/head&gt;
   &lt;body&gt;  
    &lt;div id=&#39;left&#39;&gt;
        {{template &quot;left&quot; .}}
    &lt;/div&gt;
    &lt;div id=&#39;right&#39;&gt;
        {{template &quot;right&quot; .}}
    &lt;/div&gt;
&lt;/body&gt;

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

That`s it:

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
   &lt;head&gt;&lt;/head&gt;
   &lt;body&gt;  
    &lt;div id=&#39;left&#39;&gt;
        {{template &quot;left&quot; .}}
    &lt;/div&gt;
    &lt;div id=&#39;right&#39;&gt;
        {{template &quot;right&quot; .}}
    &lt;/div&gt;
&lt;/body&gt;
&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:

确定