dongshai8330 2018-03-25 06:41
浏览 674
已采纳

Golang从模板到模板传递多个值?

My City struct is like this:

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

And Region struct is:

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

In main I try to do this:

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}}
  • 写回答

2条回答 默认 最新

  • dongxi0523 2018-03-25 10:59
    关注

    Quoting from the doc of text/template, the syntax of the {{template}} action:

    {{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.
    

    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?

    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,
        }
    }
    

    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.

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

    Output (try it on the Go Playground):

    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}]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错