Go模板中的嵌套范围

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

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:&quot;Name,omitempty&quot;`  
    Gender         string           `json:&quot;Gender,omitempty&quot;`  
    Communication  []*Communication `json:&quot;Communication,omitempty&quot;`  
}  

type Communication struct {  
    Type  string `json:&quot;Type,omitempty&quot;`  
    Value string `json:&quot;Value,omitempty&quot;`  
}  

every user will have two communication structure like

[
    {
        &quot;Type&quot;: &quot;MOBILE&quot;,
        &quot;Value&quot;: &quot;12121212&quot;
    },
    {
        &quot;Type&quot;: &quot;Email&quot;,
        &quot;Value&quot;: &quot;Some@email.com&quot;
    }
]  

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):

&lt;tbody&gt;  
{{range $key, $val := .Users}}   
&lt;td style=&quot;text-align: center;&quot;&gt;{{$val.Name}}&lt;/td&gt;  
&lt;td style=&quot;text-align: center;&quot;&gt;{{$val.Gender}}&lt;/td&gt;  
///////How to display communication values here??////////////  
{{end}}  
&lt;/tbody&gt;

答案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 &lt;td&gt;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(&quot;&quot;).Funcs(template.FuncMap{
	&quot;SliceToMap&quot;: sliceToMap,
}).Parse(src))

Then your template could be,

&lt;tbody&gt;  
{{range $key, $val := .Users}}   
&lt;td style=&quot;text-align: center;&quot;&gt;{{$val.Name}}&lt;/td&gt;  
&lt;td style=&quot;text-align: center;&quot;&gt;{{$val.Gender}}&lt;/td&gt; 

{{$comms := SliceToMap $val.Communication}}

&lt;td style=&quot;text-align: center;&quot;&gt;{{index $comms &quot;mobile&quot;}}&lt;/td&gt;
&lt;td style=&quot;text-align: center;&quot;&gt;{{index $comms &quot;email&quot;}}&lt;/td&gt;

{{end}}  
&lt;/tbody&gt;

See these in Go Playground

huangapple
  • 本文由 发表于 2016年4月5日 19:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/36425071.html
匿名

发表评论

匿名网友

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

确定