如何将多个数据传递给Go模板? [英] How to pass multiple data to Go template?

查看:78
本文介绍了如何将多个数据传递给Go模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个数据对象传递给Go Template。一个是MongoDB查询结果,另一个是整数数组。



MongoDB查询: -

  var results [] User 
sess,db:= GetDatabase()
推迟sess.Close()
c:= db.C(user)
err:= c.Find(nil).All(& results)

我想发送'结果'和一个int数组通过以下代码

  GetTemplate(list)。Execute(w,?????如果只有数据库结果,我们可以使用它作为


$ b


$ b

  GetTemplate(list)。执行(w,结果)

并且在模板中我们可以访问它{{.Name}}等等(其中Name是[] User的结构字段)



请告诉我如何传递这些数据以及如何在模板中访问它们。 您可以将多个数据打包 struct map 。



使用 struct

 类型数据结构{
结果[]用户//必须导出!
其他[] int //必须导出!
}

data:=& Data {results,[] int {1,2,3}}
if err:= GetTemplate(list)。Execute w,数据); err!= nil {
//处理错误
}

另请注意,一个新的命名类型不是必需的,你也可以使用一个匿名结构字面值,如下所示:

= struct {
结果[]用户//必须导出!
其他[] int //必须导出!
} {results,[] int {1,2,3}}



a map



  m:= map [string] interface {} {
Results:results,
Other:[] int {1,2,3},
}

if err:= GetTemplate(列表)。执行(w,m); err!= nil {
//处理错误
}

请注意,使用一个映射,它不需要使用大写的 string s作为键,例如你可以用resultsother>(但在我看来,最好使用键资本首字母,如果你将来有时候会转移到 struct ,那么你应该修改的次数更少)。



在这两种情况下,您都可以使用 {{。Results}} 参考 [] User int slice用 {{。Other}}



例如,为了覆盖用户范围:

  {{range .Results}} 
用户名:{{。Name}}
{{end}}


I want to pass two data objects to Go Template. One is a MongoDB query result and other is an integer array.

MongoDB Query:-

var results []User
sess, db := GetDatabase()
defer sess.Close()
c := db.C("user")
err := c.Find(nil).All(&results)

I want to sent 'result' and an int array through following code

GetTemplate("list").Execute(w,???????)

If there is only db result, we could use it as

GetTemplate("list").Execute(w,results)

and in template we could access it {{.Name}} etc. (where Name is a struct field of []User)

Please tell me how to pass these data and how to access them in template.

解决方案

You may wrap multiple data intended for the template in a struct or in a map.

Example with a struct:

type Data struct {
    Results []User // Must be exported!
    Other   []int  // Must be exported!
}

data := &Data{results, []int{1, 2, 3}}
if err := GetTemplate("list").Execute(w, data); err != nil {
    // Handle error
}

Also note that a new, named type is not required, you could also use an anonymous struct literal, which could look like this:

data := struct {
    Results []User // Must be exported!
    Other   []int  // Must be exported!
}{results, []int{1, 2, 3}}

Example with a map:

m := map[string]interface{}{
    "Results": results,
    "Other":   []int{1, 2, 3},
}

if err := GetTemplate("list").Execute(w, m); err != nil {
    // Handle error
}

Note that using a map, it is not required to use capitalized strings as keys, e.g. you could've used "results" and "other" too (but in my opinion it's better to use keys with capital starting letters, should you move to struct sometimes in the future, you would have less corrections to make).

In both cases you may refer to the []User results with {{.Results}} and to the additional int slice with {{.Other}}.

So for example to range over the users:

{{range .Results}}
    User name:{{.Name}}
{{end}}

这篇关于如何将多个数据传递给Go模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆