Home >Backend Development >Golang >How to Pass Multiple Values to Go Templates' {{template}} Action?

How to Pass Multiple Values to Go Templates' {{template}} Action?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-05 20:38:13776browse

How to Pass Multiple Values to Go Templates' {{template}} Action?

Passing Multiple Values Between Templates

In Go templating, the {{template}} action takes only one optional argument. To pass multiple values, they must be encapsulated within a single value.

Encapsulating Data

One approach is to use a wrapper function that accepts the desired values and returns a single value. For instance, we could create a wrapper function for our City and Region data:

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

Registering the Wrapper Function

Custom functions can be registered using Template.Funcs(). This must be done before parsing the template:

t := template.Must(template.New("cities.gohtml").Funcs(template.FuncMap{
    "Wrap": Wrap,
}).Parse(src))

Modified Template

The template can then be modified to call the Wrap() function and pass the result to the {{template}} action:

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

Example

Here's an example using the City and Region structures provided in the question:

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:

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

The above is the detailed content of How to Pass Multiple Values to Go Templates' {{template}} Action?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn