1
0
mirror of https://github.com/gohugoio/hugo.git synced 2024-05-11 05:54:58 +00:00
Files
gohugoio-hugo/content/en/functions/first.md
Bjørn Erik Pedersen b9bd35d72e Squashed 'docs/' content from commit fdea5430f
git-subtree-dir: docs
git-subtree-split: fdea5430f89dfd849d39212abdf5ace0a4763e5a
2019-10-21 10:22:28 +02:00

1.5 KiB

title, linktitle, description, godocref, date, publishdate, lastmod, categories, menu, keywords, signature, workson, hugoversion, relatedfuncs, deprecated, aliases
title linktitle description godocref date publishdate lastmod categories menu keywords signature workson hugoversion relatedfuncs deprecated aliases
first first Slices an array to only the first _N_ elements. 2017-02-01 2017-02-01 2017-02-01
functions
docs
parent
functions
iteration
first LIMIT COLLECTION
lists
taxonomies
terms
groups
after
last
false

first works in a similar manner to the limit keyword in SQL. It reduces the array to only the first N elements. It takes the array and number of elements as input.

first takes two arguments:

  1. number of elements
  2. array or slice of maps or structs

{{< code file="layout/_default/section.html" >}} {{ range first 10 .Pages }} {{ .Render "summary" }} {{ end }} {{< /code >}}

Note: Exclusive to first, LIMIT can be '0' to return an empty array.

first and where Together

Using first and where together can be very powerful. Below snippet gets a list of posts only from main sections, sorts it by the title parameter, and then ranges through only the first 5 posts in that list:

{{< code file="first-and-where-together.html" >}} {{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections).ByTitle }} {{ .Content }} {{ end }} {{< /code >}}