相关疑难解决方法(0)

如何将多个值从模板传递到模板?

我的City结构是这样的:

type City struct {
    ID      int
    Name    string
    Regions []Region
}  
Run Code Online (Sandbox Code Playgroud)

Region结构是:

type Region struct {
    ID               int
    Name             string
    Shops            []Destination
    Masters          []Master
    EducationCenters []Destination
}
Run Code Online (Sandbox Code Playgroud)

主要是我尝试这样做:

tpl.ExecuteTemplate(resWriter,"cities.gohtml",CityWithSomeData)
Run Code Online (Sandbox Code Playgroud)

是否可以在模板内部做这样的事情?

{{range .}}
        {{$city:=.Name}}
            {{range .Regions}}
                      {{$region:=.Name}}
                      {{template "data" .Shops $city $region}}
            {{end}}
{{end}}
Run Code Online (Sandbox Code Playgroud)

struct loops go slice go-templates

5
推荐指数
1
解决办法
4174
查看次数

标签 统计

go ×1

go-templates ×1

loops ×1

slice ×1

struct ×1