4

範囲が空の場合、条件付きで何か他のものを表示するにはどうすればよいですか?

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

範囲が空の場合、HTML の別のブロックを表示したい。

4

1 に答える 1

6

使用{{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}}
于 2016-05-28T23:00:17.253 に答える