如何知道范围是否为空?

时间:2016-05-28 22:59:13

标签: go go-templates

如果我的范围是空的,我怎么能有条件地显示其他东西?

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

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

1 个答案:

答案 0 :(得分:5)

使用{{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}}
相关问题