How to pass multiple objects to Go html template

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

How to pass multiple objects to Go html template

问题

这是我的对象数组:

type PeopleCount []struct{
   Name  string
   Count int
}

type Consultation []struct{
   Name          string
   Opd_count     int
   Opinion_count int
   Req_count     int
}

你应该如何将这两个对象传递给 HTML 模板并在表格中排列它们?

英文:

Here are my array of objects,

type PeopleCount []struct{
   Name  string
   Count int
}

type Consultation []struct{
   Name          string
   Opd_count     int
   Opinion_count int
   Req_count     int
}

How should I pass both the objects to html template and arrange them in table?

答案1

得分: 5

定义一个匿名结构体,其中包含人数和咨询的字段,并将该结构体传递给模板的Execute方法:

var data = struct {
    PeopleCounts  []PeopleCount
    Consultations []Consultation
}{
    PeopleCounts:  p,
    Consultations: c,
}
err := t.Execute(w, &data)
if err != nil {
    // 处理错误
}

在模板中使用这些字段:

{{range .PeopleCounts}}{{.Name}}
{{end}}
{{range .Consultations}}{{.Name}}
{{end}}

Playground示例

你可以为模板数据声明一个命名类型。使用匿名类型声明的优点是模板数据的知识局限于调用模板的函数中。

你也可以使用map而不是类型:

err := t.Execute(w, map[string]interface{}{"PeopleCounts": p, "Consultations": c})
if err != nil {
    // 处理错误
}

使用map的缺点是模板中的拼写错误可能不会导致错误。例如,``{{range .PopleConts}}{{end}}` 会静默地不起作用。

上述代码假设PeopleCount和Consultation是结构体类型,而不是匿名结构体类型的切片:

type PeopleCount struct {
    Name  string
    Count int
}

type Consultation struct {
    Name          string
    Opd_count     int
    Opinion_count int
    Req_count     int
}

给元素命名类型通常比给切片命名类型更方便。

英文:

Define an anonymous struct with fields for people count and consultations and pass the struct to the template Execute method:

var data = struct {
	PeopleCounts  []PeopleCount
	Consultations []Consultation
}{
	PeopleCounts:  p,
	Consultations: c,
}
err := t.Execute(w, &data)
if err != nil {
    // handle error
}

Use these fields in the template:

{{range .PeopleCounts}}{{.Name}}
{{end}}
{{range .Consultations}}{{.Name}}
{{end}}

Playground example

You can declare a named type for the template data. The advantage of the anonymous type declaration is that knowledge of the template data is localized to the function that invokes the template.

You can also use a map instead of a type:

err := t.Execute(w, map[string]interface{}{"PeopleCounts": p, "Consultations": c})
if err != nil {
    // handle error
}

The disadvantage of using a map is that a typo in the template may not result in an error. For example, ``{{range .PopleConts}}{{end}}` silent does nothing.

The code above assumes that PeopleCount and Consultation are struct types and not slices of anonymous struct types:

type PeopleCount struct {
  Name  string
  Count int
}

type Consultation struct {
  Name          string
  Opd_count     int
  Opinion_count int
  Req_count     int
}

It's usually more convenient to give the element a named type than to give the slice a named type.

答案2

得分: 1

如果你愿意,可以定义一个未导出的结构体,其中包含人数和咨询的字段,并将该结构体传递给模板的Execute方法:

type viewModel struct {
    PeopleCounts  []PeopleCount
    Consultations []Consultation
}

// ...

var data = viewModel{
    PeopleCounts:  p,
    Consultations: c,
}
err := t.Execute(w, &data)
if err != nil {
    // 处理错误
}

这种方法与@Bravada的答案基本相似。只是个人口味的问题,是明确使用视图模型类型还是匿名使用。

英文:

If you prefer, define an unexported struct with fields for people count and consultations and pass the struct to the template Execute method:

type viewModel struct {
	PeopleCounts  []PeopleCount
	Consultations []Consultation
}

// ...

var data = viewModel{
	PeopleCounts:  p,
	Consultations: c,
}
err := t.Execute(w, &data)
if err != nil {
    // handle error
}

This approach is broadly similar to @Bravada's answer. It's merely a matter of personal taste whether to use a view-model type explicitly or anonymously.

huangapple
  • 本文由 发表于 2015年9月11日 23:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/32527241.html
匿名

发表评论

匿名网友

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

确定