遍历模板中的地图 [英] Iterating through map in template
问题描述
我正在尝试显示健身课程清单(瑜伽,普拉提等).对于每个班级类型,都有几个班级,因此我想将所有瑜伽班和所有普拉提班归为一组.
I am trying to display a list gym classes (Yoga, Pilates etc). For each class type there are several classes, so I want to group all the Yoga classes, and all the Pilates classes and so on.
我使用了此功能来切片并绘制地图
I made this function to take a slice and make a map of it
func groupClasses(classes []entities.Class) map[string][]entities.Class {
classMap := make(map[string][]entities.Class)
for _, class := range classes {
classMap[class.ClassType.Name] = append(classMap[class.ClassType.Name], class)
}
return classMap
}
根据 http://golang.org/pkg/text/template,问题现在是如何遍历它/,您需要以.Key
格式访问它,我不知道键(除非我也将一部分键传递到模板中).如何在我的视图中解包此地图.
The problem is now how can I iterate through it, according to http://golang.org/pkg/text/template/, you need to access it in .Key
format, I don't know the keys (unless I also passed a slice of keys into the template). How do I unpack this map in my view.
我目前只有
{{ . }}
显示如下内容:
map[Pilates:[{102 PILATES ~/mobifit/video/ocen.mpg 169 40 2014-05-03 23:12:12 +0000 UTC 2014-05-03 23:12:12 +0000 UTC 1899-12-30 00:00:00 +0000 UTC {PILATES Pilates 1 2014-01-22 21:46:16 +0000 UTC} {1 leebrooks0@gmail.com password SUPERADMIN Lee Brooks {Male true} {1990-07-11 00:00:00 +0000 UTC true} {1.85 true} {88 true} 2014-01-22 21:46:16 +0000 UTC {0001-01-01 00:00:00 +0000 UTC false} {0001-01-01 00:00:00 +0000 UTC false} {0001-01-01 00:00:00 +0000 UTC false}} [{1 Mat 2014-01-22 21:46:16 +0000 UTC}]} {70 PILATES ~/mobifit/video/ocen.mpg 119 66 2014-03-31 15:12:12 +0000 UTC 2014-03-31 15:12:12 +0000 UTC 1899-12-30 00:00:00 +0000 UTC
推荐答案
检查变量部分在Go模板文档中.范围可以声明两个变量,以逗号分隔.以下应该可以工作:
Check the Variables section in the Go template docs. A range may declare two variables, separated by a comma. The following should work:
{{ range $key, $value := . }}
<li><strong>{{ $key }}</strong>: {{ $value }}</li>
{{ end }}
这篇关于遍历模板中的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!