英文:
nested range in Go template
问题
我有这样的结构:
type Users struct {
Name string `json:"Name,omitempty"`
Gender string `json:"Gender,omitempty"`
Communication []*Communication `json:"Communication,omitempty"`
}
type Communication struct {
Type string `json:"Type,omitempty"`
Value string `json:"Value,omitempty"`
}
每个用户将有两个通信结构,如下所示:
[
{
"Type": "MOBILE",
"Value": "12121212"
},
{
"Type": "Email",
"Value": "Some@email.com"
}
]
在我的模板中,我想将它们显示在一个表格中。我可以获取到用户结构的值,但无法获取到通信结构的值。
HTML模板文件(部分代码):
<tbody>
{{range $key, $val := .Users}}
<td style="text-align: center;">{{$val.Name}}</td>
<td style="text-align: center;">{{$val.Gender}}</td>
///////如何在这里显示通信值??////////////
{{end}}
</tbody>
请注意,这只是一个示例代码,实际上你需要根据你的需求进行适当的修改。
英文:
I have structure like these
type Users struct{
Name string `json:"Name,omitempty"`
Gender string `json:"Gender,omitempty"`
Communication []*Communication `json:"Communication,omitempty"`
}
type Communication struct {
Type string `json:"Type,omitempty"`
Value string `json:"Value,omitempty"`
}
every user will have two communication structure like
[
{
"Type": "MOBILE",
"Value": "12121212"
},
{
"Type": "Email",
"Value": "Some@email.com"
}
]
In my template i want to display them in a table. iam getting the User structure values, but could not get the communication structure values
HTML Template file (partial code):
<tbody>
{{range $key, $val := .Users}}
<td style="text-align: center;">{{$val.Name}}</td>
<td style="text-align: center;">{{$val.Gender}}</td>
///////How to display communication values here??////////////
{{end}}
</tbody>
答案1
得分: 2
您可以像访问其他字段一样访问Communication字段。
{{$val.Communication}}
由于您希望将每个条目分别放在单独的<td>
中,如果您可以将它们放在map
而不是slice
中,那将更容易。您可以使用以下函数来实现。
sliceToMap := func(s []*Communication) map[string]string {
comms := map[string]string{}
for _, c := range s {
comms[c.Type] = c.Value
}
return comms
}
您可以将其注册为自定义函数以在模板中使用。
t := template.Must(template.New("").Funcs(template.FuncMap{
"SliceToMap": sliceToMap,
}).Parse(src))
然后您的模板可以是这样的:
<tbody>
{{range $key, $val := .Users}}
<td style="text-align: center;">{{$val.Name}}</td>
<td style="text-align: center;">{{$val.Gender}}</td>
{{$comms := SliceToMap $val.Communication}}
<td style="text-align: center;">{{index $comms "mobile"}}</td>
<td style="text-align: center;">{{index $comms "email"}}</td>
{{end}}
</tbody>
在Go Playground中查看示例。
英文:
You can access the Communication field just like other fields.
{{$val.Communication}}
Since you want each these entries in separate <td>
s its easier if you could put them in a map
instead of a slice
. You could use a function like below for that.
sliceToMap := func(s []*Communication) map[string]string {
comms := map[string]string{}
for _, c := range s {
comms[c.Type] = c.Value
}
return comms
}
You can register this as a custom function to be used in the template,
t := template.Must(template.New("").Funcs(template.FuncMap{
"SliceToMap": sliceToMap,
}).Parse(src))
Then your template could be,
<tbody>
{{range $key, $val := .Users}}
<td style="text-align: center;">{{$val.Name}}</td>
<td style="text-align: center;">{{$val.Gender}}</td>
{{$comms := SliceToMap $val.Communication}}
<td style="text-align: center;">{{index $comms "mobile"}}</td>
<td style="text-align: center;">{{index $comms "email"}}</td>
{{end}}
</tbody>
See these in Go Playground
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论