我正在尝试展示列表健身课程(瑜伽,普拉提等).对于每个类类型,有几个类,所以我想分组所有瑜伽课程,以及所有普拉提课程等等.
我做了这个函数来切片并制作它的地图
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
}
Run Code Online (Sandbox Code Playgroud)
问题是现在如何迭代它,根据http://golang.org/pkg/text/template/,你需要以.Key格式访问它,我不知道密钥(除非我也传递了一个切片键入模板).如何在视图中解压缩此地图.
我目前所拥有的只是
{{ . }}
Run Code Online (Sandbox Code Playgroud)
它显示如下:
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 …Run Code Online (Sandbox Code Playgroud) 我有一个片段: Keys []* datastore.Key
我怎么能在模板文件中索引其中一个?我猜{{.Keys[3] }},但这不起作用,我搜索了很多,但没有任何线索.
任何建议都会受到欢迎,谢谢.
在golang的template/html包中,我可以{{ if .loggedIn }}用来检查登录是否为真.
如何在不使用ne或eq的情况下检查.loggedIn是否为false?
例如,我正在寻找类似的东西
{{ if !.loggedIn }}
<h1>Not logged in</h1>
{{ end }}
Run Code Online (Sandbox Code Playgroud) 让我说我有
type Person struct {
Name string
}
func (p *Person) Label() string {
return "This is " + p.Name
}
Run Code Online (Sandbox Code Playgroud)
如何从html /模板中使用此方法?在我的模板中我需要这样的东西:
{{ .Label() }}
Run Code Online (Sandbox Code Playgroud) 我正在使用AngularJS作为前端JS库,在Revel框架中使用Go模板来生成后端的标记.
但无论你去角的使用{{,并}}在他们的模板分隔符.如何在Go中将它们传递给AngularJS?
在里面with或者range,范围.改变了.你如何访问调用范围?
我有这个结构:
const (
paragraph_hypothesis = 1<<iota
paragraph_attachment = 1<<iota
paragraph_menu = 1<<iota
)
type Paragraph struct {
Type int // paragraph_hypothesis or paragraph_attachment or paragraph_menu
}
Run Code Online (Sandbox Code Playgroud)
我想以Type依赖的方式显示我的段落.
我找到的唯一解决方案是基于专用函数,如isAttachment测试TypeGo和嵌套{{if}}:
{{range .Paragraphs}}
{{if .IsAttachment}}
-- attachement presentation code --
{{else}}{{if .IsMenu}}
-- menu --
{{else}}
-- default code --
{{end}}{{end}}
{{end}}
Run Code Online (Sandbox Code Playgroud)
事实上,我有更多类型,这使得它甚至更奇怪,使用IsSomething函数和带有函数的模板混乱{{end}}.
什么是干净的解决方案?go模板中是否有一些switch或if/elseif/else解决方案?还是以完全不同的方式处理这些案件?
当我尝试使用Funcs和时,它注意到Go模板有一个奇怪的东西FuncMap.以下代码按预期工作:
buffer := bytes.NewBufferString("")
funcMap := template.FuncMap{
"label": strings.Title,
}
t, _ := template.New("alex").Funcs(funcMap).Parse("{{label \"alex\"}}")
t.Execute(buffer, "")
return string(buffer.Bytes()) //=> "Alex"
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将模板放在一个文件中时,它不起作用(Execute()说:) "alex" is an incomplete or empty template:
t, _ := template.New("alex").Funcs(funcMap).ParseFiles("template.html")
Run Code Online (Sandbox Code Playgroud)
使用template.html:
{{label \"alex\"}}
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?这是一个错误吗?是否有更简单的方法在模板中使用方法/功能?
在Go模板中,有时将正确数据传递到正确模板的方式对我来说感觉很尴尬.使用管道参数调用模板看起来就像调用只有一个参数的函数.
假设我有一个关于Gophers的Gophers网站.它有一个主页主模板和一个实用程序模板来打印Gophers列表.
http://play.golang.org/p/Jivy_WPh16
输出:
*The great GopherBook* (logged in as Dewey)
[Most popular]
>> Huey
>> Dewey
>> Louie
[Most active]
>> Huey
>> Louie
[Most recent]
>> Louie
Run Code Online (Sandbox Code Playgroud)
现在我想在子模板中添加一些上下文:在列表中以不同的方式格式化名称"Dewey",因为它是当前登录用户的名称.但我无法直接传递名称,因为只有一个可能的"点"参数管道!我能做什么?
当我调用Go模板函数输出HTML时,它会显示ZgotmplZ.
示例代码:
http://play.golang.org/p/tfuJa_pFkm
package main
import (
"html/template"
"os"
)
func main() {
funcMap := template.FuncMap{
"printSelected": func(s string) string {
if s == "test" {
return `selected="selected"`
}
return ""
},
"safe": func(s string) template.HTML {
return template.HTML(s)
},
}
template.Must(template.New("Template").Funcs(funcMap).Parse(`
<option {{ printSelected "test" }} {{ printSelected "test" | safe }} >test</option>
`)).Execute(os.Stdout, nil)
}
Run Code Online (Sandbox Code Playgroud)
输出:
<option ZgotmplZ ZgotmplZ >test</option>
Run Code Online (Sandbox Code Playgroud)