2 回答
TA贡献1872条经验 获得超3个赞
所以基本上你只想显示文本 if .Subscriber.Attribs.Prois 提供和 is false。所以做一个比较:
{{ if eq false .Subscriber.Attribs.Pro }}
You are not a Pro user!
{{ end }}
我们可以像这样测试它:
t := template.Must(template.New("").Parse("{{ if eq false .Pro }}You are not a Pro user!\n{{ end }}"))
fmt.Println("Doesn't exist:")
if err := t.Execute(os.Stdout, nil); err != nil {
panic(err)
}
fmt.Println("Pro is false:")
m := map[string]interface{}{
"Pro": false,
}
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
fmt.Println("Pro is true:")
m["Pro"] = true
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
输出将是(在Go Playground上尝试):
Doesn't exist:
Pro is false:
You are not a Pro user!
Pro is true:
如您所见,块的主体仅在显式设置为{{if}}时才会执行。Profalse
TA贡献1815条经验 获得超13个赞
使用附带的Sprig hasKey 函数:
{{if hasKey .Subscriber.Attribs "Pro"}}
{{ if not .Subscriber.Attribs.Pro }}
You are not a Pro user!
{{ end }}
{{ end }}
- 2 回答
- 0 关注
- 124 浏览
添加回答
举报