如何在模板之间传递多个值? [英] How to pass multiple values from template to template?

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

问题描述

我的City结构是这样的:

type City struct {
    ID      int
    Name    string
    Regions []Region
}  

Region结构是:

type Region struct {
    ID               int
    Name             string
    Shops            []Destination
    Masters          []Master
    EducationCenters []Destination
}

主要,我尝试这样做:

tpl.ExecuteTemplate(resWriter,"cities.gohtml",CityWithSomeData)

是否可以在模板内部执行类似的操作?

Is it possible to do something like this inside template?

{{range .}}
        {{$city:=.Name}}
            {{range .Regions}}
                      {{$region:=.Name}}
                      {{template "data" .Shops $city $region}}
            {{end}}
{{end}}

推荐答案

text/template {{template}}操作的语法:

{{template "name"}}
    The template with the specified name is executed with nil data.

{{template "name" pipeline}}
    The template with the specified name is executed with dot set
    to the value of the pipeline.

这意味着您可以将一个可选数据传递给模板执行,而不是更多.如果要传递多个值,则必须将它们包装为传递的某个单个值.有关详细信息,请参见如何将多个数据传递到Go模板?

This means you may pass one optional data to the template execution, not more. If you want to pass multiple values, you have to wrap them into some single value you pass. For details, see How to pass multiple data to Go template?

因此,我们应该将这些数据包装到结构或映射中.但是我们不能在模板中编写Go代码.我们可能要做的就是注册一个函数,将这些数据传递给该函数,该函数可以进行打包"并返回单个值,现在我们可以将其传递给{{template}}动作.

So we should wrap those data into a struct or a map. But we can't write Go code in a template. What we may do is register a function to which we pass these data, and the function may do the "packing" and return a single value which now we can pass to the {{template}} action.

下面是一个示例包装程序,将这些程序简单地包装到地图中:

Here's an example wrapper which simply packs these into a map:

func Wrap(shops []Destination, cityName, regionName string) map[string]interface{} {
    return map[string]interface{}{
        "Shops":      shops,
        "CityName":   cityName,
        "RegionName": regionName,
    }
}

可以使用 Template.Funcs() 方法注册自定义功能,并且不要忘记在解析模板文本之前必须执行此操作.

Custom functions can be registered using the Template.Funcs() method, and don't forget you have to do this before you parse the template text.

这是一个经过修改的模板,该模板调用此Wrap()函数以生成单个值:

Here's a modified template which calls this Wrap() function to produce a single value:

const src = `
{{define "data"}}
    City: {{.CityName}}, Region: {{.RegionName}}, Shops: {{.Shops}}
{{end}}
{{- range . -}}
        {{$city:=.Name}}
        {{- range .Regions -}}
              {{$region:=.Name}}
              {{- template "data" (Wrap .Shops $city $region) -}}
        {{end}}
{{- end}}`

这是一个运行中的示例,显示了这些示例的作用:

And here's a runnable example showing these in action:

t := template.Must(template.New("cities.gohtml").Funcs(template.FuncMap{
    "Wrap": Wrap,
}).Parse(src))
CityWithSomeData := []City{
    {
        Name: "CityA",
        Regions: []Region{
            {Name: "CA-RA", Shops: []Destination{{"CA-RA-SA"}, {"CA-RA-SB"}}},
            {Name: "CA-RB", Shops: []Destination{{"CA-RB-SA"}, {"CA-RB-SB"}}},
        },
    },
    {
        Name: "CityB",
        Regions: []Region{
            {Name: "CB-RA", Shops: []Destination{{"CB-RA-SA"}, {"CB-RA-SB"}}},
            {Name: "CB-RB", Shops: []Destination{{"CB-RB-SA"}, {"CB-RB-SB"}}},
        },
    },
}
if err := t.ExecuteTemplate(os.Stdout, "cities.gohtml", CityWithSomeData); err != nil {
    panic(err)
}

输出(在游乐场上尝试):

City: CityA, Region: CA-RA, Shops: [{CA-RA-SA} {CA-RA-SB}]

City: CityA, Region: CA-RB, Shops: [{CA-RB-SA} {CA-RB-SB}]

City: CityB, Region: CB-RA, Shops: [{CB-RA-SA} {CB-RA-SB}]

City: CityB, Region: CB-RB, Shops: [{CB-RB-SA} {CB-RB-SB}]

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

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