How to pass multiple values from template to template?
Asked Answered
S

3

6

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}}
Shere answered 25/3, 2018 at 6:41 Comment(1)
Possible duplicate of #18276673Aurlie
B
9

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}]
Booma answered 25/3, 2018 at 10:59 Comment(1)
That is the one which I expected. ThanksShere
D
0

I guess CityWithSomeData is a slice, if so have a try like that:

type viewModel struct {
    List   []City
}

then, in your template:

{{range .List}}
        {{$city:=.Name}}
            {{range .Regions}}
                      {{$region:=.Name}}
                      {{template "data" .Shops $city $region}}
            {{end}}
{{end}}
Decelerate answered 25/3, 2018 at 6:53 Comment(1)
City is the struct as show above. I wonder How do I access these $city and $region variables in data templateShere
S
0

You need the generic dict function that allows to build a map[string]interface{} from a list of parameters.

See https://mcmap.net/q/350797/-calling-a-template-with-several-pipeline-parameters

Sheryl answered 1/6, 2022 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.