golang template.Execute和结构体嵌入

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

golang template.Execute and struct embedding

问题

我有一个用Go编写的小网站项目,你可以在其中存储链接,但我遇到了一个问题:

该网站有许多不同的页面,显示不同的信息,因此您需要传递不同类型的结构体来执行模板。但是每个页面还需要像用户名和标签这样的信息,这些信息显示在侧边栏中。我尝试了一种方法,而不是为每个页面都创建全新的结构体类型。

http://play.golang.org/p/VNfD6i8p_N

type Page interface {
	Name() string
}

type GeneralPage struct {
	PageName string
}

func (s GeneralPage) Name() string {
	return s.PageName
}

type PageRoot struct {
	Page
	Tags       []string
	IsLoggedIn bool
	Username   string
}

type ListPage struct {
	Page
	Links     []Link
	IsTagPage bool
	Tag       string
}

type GalleryPage struct {
	Page
	Image    Link
	Next     int
	Previous int
}

但是当我执行模板时,出现错误:"fp.tmpl" at <.Links>: can't evaluate field Links in type main.Page

错误发生的模板部分:

  {{with .Page}}
  {{range .Links}}
  <tr>
    <td>{{if .IsImage}}<img src="{{.Url}}" />{{end}}</td>
    <td>{{.Name}}</td>
    <td>{{.Url}}</td>
    <td>{{.TagsString}}</td>
  </tr>
  {{end}}
  {{end}}

{{.Name}} 不起作用。(它是从GeneralPage嵌入的函数)

英文:

I have a little website project written go where you can store links, and I ran into a problem :

The website has many different pages which show different information so you need to pass template.Execute a different kind of a struct. But every page also needs info like username and tags, which are displayed in sidebar. I tried to make something like this instead of just making completely new struct type for each page.

http://play.golang.org/p/VNfD6i8p_N

type Page interface {
	Name() string
}

type GeneralPage struct {
	PageName string
}

func (s GeneralPage) Name() string {
	return s.PageName
}

type PageRoot struct {
	Page
	Tags       []string
	IsLoggedIn bool
	Username   string
}

type ListPage struct {
	Page
	Links     []Link
	IsTagPage bool
	Tag       string
}

type GalleryPage struct {
	Page
	Image    Link
	Next     int
	Previous int
}

But I get an error when I execute the template: "fp.tmpl" at <.Links>: can't evaluate field Links in type main.Page

The part of the template where the error occurs:

  {{with .Page}}
  {{range .Links}}
  <tr>
    <td>{{if .IsImage}}<img src="{{.Url}}" />{{end}}</td>
    <td>{{.Name}}</td>
    <td>{{.Url}}</td>
    <td>{{.TagsString}}</td>
  </tr>
  {{end}}
  {{end}}

And {{.Name}} doesn't work. (It's the function embedded from GeneralPage)

答案1

得分: 1

你正在嵌入Page接口,但你需要的是GeneralPage。也许你可以使用map[string]interface{}来存储你的数据(然后在模板中检查是否为非nil),这样更容易。但你可以共享主要布局,只需更改细节(就像主页面)。请参考http://golang.org/pkg/text/template/#example_Template_share。

英文:

You're embeding the Page interface, but what you need is GeneralPage.
Maybe you can use a map[string]interface{} to store your data (and then check if not-nil in your template), it easier.
But you can share the main layout and just change the detail (like a master page).
Look at http://golang.org/pkg/text/template/#example_Template_share

huangapple
  • 本文由 发表于 2013年12月18日 01:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/20640950.html
匿名

发表评论

匿名网友

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

确定