go - 如何知道范围是否为空?

标签 go go-templates

如果范围为空,我如何有条件地显示其他内容?

{{range .Users}}
...
{{end}}

如果范围为空,我想显示不同的 HTML block 。

最佳答案

使用{{range pipeline}} T1 {{else}} T0 {{end}} :

{{range pipeline}} T1 {{else}} T0 {{end}}
        The value of the pipeline must be an array, slice, map, or channel.
        If the value of the pipeline has length zero, dot is unaffected and
        T0 is executed; otherwise, dot is set to the successive elements
        of the array, slice, or map and T1 is executed.

例子:

{{range .Users}}
...
{{else}}
<p>No users</p>
{{end}}

关于go - 如何知道范围是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504510/

相关文章:

提供静态文件时出现 golang 错误

performance - 我应该在每个 http 请求上调用 template.ParseFiles(...) 还是只从主函数调用一次?

go - 如何将当前年份添加到 Go 模板中?

go - 在 Hugo 部分模板中,如何访问辅助/附加参数?

go - 有没有办法约束(通用)类型参数?

amazon-web-services - Golang S3铲斗尺寸

parsing - 允许多次读取 http.Request.Body 的正确方法是什么

http - 限制从 HTTP 响应中读取的字节数

go - golang 中的渲染模板

go - Hyperledger Fabric V1.0 中日期范围的复合键形成