HUGO
Menu
GitHub 90633 stars Mastodon

collections.Last

Returns the last N elements of the given slice or string.
Syntax
collections.Last N SLICE|STRING
Returns
any
Alias
last

Usage

The collections.Last function returns the last N elements of the given slice or string. If N is greater than the length of the slice or string, the function returns the entire slice or string. If N is zero, the function returns an empty slice or string.

When you pass a string, the function returns the last N bytes, not the last N characters. A character may consist of more than one byte, so the returned string may begin with an incomplete character.

Examples

{{ slice "a" "b" "c" | collections.Last 1 }} → [c]
{{ slice "a" "b" "c" | collections.Last 2 }} → [b c]

With a string:

{{ "abc" | collections.Last 1 }} → c
{{ "abc" | collections.Last 2 }} → bc

With a string that contains multibyte characters:

{{ "Schön" | collections.Last 1 }} → n
{{ "Schön" | collections.Last 2 }} → \xb6n
{{ "Schön" | collections.Last 3 }} → ön

With a page collection:

{{ range collections.Last 5 .Pages }}
  {{ .Render "summary" }}
{{ end }}

Filtering a page collection with the collections.Where function before returning the last five pages:

{{ range where .Pages "Section" "articles" | collections.Last 5 }}
  {{ .Render "summary" }}
{{ end }}