hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

first.md (1501B)

    1 ---
    2 title: first
    3 linktitle: first
    4 description: "Slices an array to only the first _N_ elements."
    5 date: 2017-02-01
    6 publishdate: 2017-02-01
    7 lastmod: 2017-02-01
    8 categories: [functions]
    9 menu:
   10   docs:
   11     parent: "functions"
   12 keywords: [iteration]
   13 signature: ["first LIMIT COLLECTION"]
   14 workson: [lists,taxonomies,terms,groups]
   15 hugoversion:
   16 relatedfuncs: [after,last]
   17 deprecated: false
   18 aliases: []
   19 ---
   20 
   21 `first` works in a similar manner to the [`limit` keyword in
   22 SQL][limitkeyword]. It reduces the array to only the `first N`
   23 elements. It takes the array and number of elements as input.
   24 
   25 `first` takes two arguments:
   26 1. `number of elements`
   27 2. `array` *or* `slice of maps or structs`
   28 
   29 {{< code file="layout/_default/section.html" >}}
   30 {{ range first 10 .Pages }}
   31     {{ .Render "summary" }}
   32 {{ end }}
   33 {{< /code >}}
   34 
   35 *Note: Exclusive to `first`, LIMIT can be '0' to return an empty array.*
   36 
   37 ## `first` and `where` Together
   38 
   39 Using `first` and [`where`][wherefunction] together can be very
   40 powerful. Below snippet gets a list of posts only from [**main
   41 sections**][mainsections], sorts it by the `title` parameter, and then
   42 ranges through only the first 5 posts in that list:
   43 
   44 {{< code file="first-and-where-together.html" >}}
   45 {{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections).ByTitle }}
   46    {{ .Content }}
   47 {{ end }}
   48 {{< /code >}}
   49 
   50 
   51 [limitkeyword]: https://www.techonthenet.com/sql/select_limit.php
   52 [wherefunction]: /functions/where/
   53 [mainsections]: /functions/where/#mainsections