collections.First
Syntax
collections.First LIMIT COLLECTION
Returns
any
Alias
first
first的工作方式类似于SQL中的limit关键字。它将数组减少到只包含前N个元素。它接受数组和元素数量作为输入。
first有两个参数:
元素数量数组或由映射或结构体组成的切片
layout/_default/section.html
{{ range first 10 .Pages }}
{{ .Render "summary" }}
{{ end }}注意:仅适用于first,LIMIT可以为'0’以返回一个空数组。
first和where结合使用
将first和where结合在一起可以非常强大。下面的代码片段从[主要部分]获取帖子列表,按title参数进行排序,然后遍历该列表中的前5个帖子:
first-and-where-together.html
{{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections).ByTitle }}
{{ .Content }}
{{ end }}