diff --git a/content/en/functions/collections/Shuffle.md b/content/en/functions/collections/Shuffle.md index e752fda6e..60af0a1ba 100644 --- a/content/en/functions/collections/Shuffle.md +++ b/content/en/functions/collections/Shuffle.md @@ -12,7 +12,6 @@ aliases: [/functions/shuffle] --- ```go-html-template -{{ collections.Shuffle (seq 1 2 3) }} → [3 1 2] {{ collections.Shuffle (slice "a" "b" "c") }} → [b a c] ``` @@ -28,21 +27,3 @@ To render an unordered list of 5 random pages from a page collection: {{ end }} ``` - -For better performance with large collections, use the [`math.Rand`] and [`collections.Index`] functions instead: - -```go-html-template - -``` - -[`collections.Index`]:/functions/collections/indexfunction/ -[`math.Rand`]: /functions/math/rand/ diff --git a/content/en/functions/debug/Timer.md b/content/en/functions/debug/Timer.md index c2cd59211..74395b130 100644 --- a/content/en/functions/debug/Timer.md +++ b/content/en/functions/debug/Timer.md @@ -18,7 +18,7 @@ The timer starts when you instantiate it, and stops when you call its `Stop` met ```go-html-template {{ $t := debug.Timer "TestSqrt" }} -{{ range seq 2000 }} +{{ range 2000 }} {{ $f := math.Sqrt . }} {{ end }} {{ $t.Stop }} diff --git a/content/en/functions/go-template/range.md b/content/en/functions/go-template/range.md index a06907c79..50f714140 100644 --- a/content/en/functions/go-template/range.md +++ b/content/en/functions/go-template/range.md @@ -11,7 +11,7 @@ params: aliases: [/functions/range] --- -{{% include "/_common/functions/truthy-falsy.md" %}} +The collection may be a slice, a map, or an integer. ```go-html-template {{ $s := slice "foo" "bar" "baz" }} @@ -40,19 +40,22 @@ Within a range block: At the top of a page template, the [context](g) (the dot) is a `Page` object. Within the `range` block, the context is bound to each successive element. -With this contrived example that uses the [`seq`] function to generate a slice of integers: +With this contrived example: ```go-html-template -{{ range seq 3 }} - {{ .Title }} +{{ $s := slice "foo" "bar" "baz" }} +{{ range $s }} + {{ .Title }} {{ end }} ``` Hugo will throw an error: - can't evaluate field Title in type int +```text +can't evaluate field Title in type int +``` -The error occurs because we are trying to use the `.Title` method on an integer instead of a `Page` object. Within the `range` block, if we want to render the page title, we need to get the context passed into the template. +The error occurs because we are trying to use the `.Title` method on a string instead of a `Page` object. Within the `range` block, if we want to render the page title, we need to get the context passed into the template. > [!note] > Use the `$` to get the context passed into the template. @@ -60,15 +63,18 @@ The error occurs because we are trying to use the `.Title` method on an integer This template will render the page title three times: ```go-html-template -{{ range seq 3 }} - {{ $.Title }} +{{ $s := slice "foo" "bar" "baz" }} +{{ range $s }} + {{ $.Title }} {{ end }} ``` > [!note] > Gaining a thorough understanding of context is critical for anyone writing template code. -## Array or slice of scalars +## Examples + +### Slice of scalars This template code: @@ -121,7 +127,7 @@ Is rendered to:

2: baz

``` -## Array or slice of maps +### Slice of maps This template code: @@ -144,7 +150,7 @@ Is rendered to:

Joey is 24

``` -## Array or slice of pages +### Slice of pages This template code: @@ -162,7 +168,7 @@ Is rendered to:

Article 1

``` -## Maps +### Maps This template code: @@ -182,9 +188,32 @@ Is rendered to: Unlike ranging over an array or slice, Hugo sorts by key when ranging over a map. +### Integers + +{{< new-in 0.123.0 />}} + +Ranging over a positive integer `n` executes the block `n` times, with the context starting at zero and incrementing by one in each iteration. + +```go-html-template +{{ $s := slice }} +{{ range 1 }} + {{ $s = $s | append . }} +{{ end }} +{{ $s }} → [0] +``` + +```go-html-template +{{ $s := slice }} +{{ range 3 }} + {{ $s = $s | append . }} +{{ end }} +{{ $s }} → [0 1 2] +``` + +Ranging over a non-positive integer executes the block zero times. + {{% include "/_common/functions/go-template/text-template.md" %}} [`break`]: /functions/go-template/break/ [`continue`]: /functions/go-template/continue/ [`else`]: /functions/go-template/else/ -[`seq`]: /functions/collections/seq/ diff --git a/content/en/templates/introduction.md b/content/en/templates/introduction.md index 8898ee456..a5b33223a 100644 --- a/content/en/templates/introduction.md +++ b/content/en/templates/introduction.md @@ -400,14 +400,14 @@ See documentation for [`range`], [`else`], and [`end`]. {{ end }} ``` -Use the [`seq`] function to loop a specified number of times: +To loop a specified number of times: ```go-html-template -{{ $total := 0 }} -{{ range seq 4 }} - {{ $total = add $total . }} +{{ $s := slice }} +{{ range 3 }} + {{ $s = $s | append . }} {{ end }} -{{ $total }} → 10 +{{ $s }} → [0 1 2] ``` ### Rebind context @@ -513,7 +513,6 @@ In the template example above, each of the keys is a valid identifier. For examp [`range`]: /functions/go-template/range/ [`range`]: /functions/go-template/range/ [`safeHTML`]: /functions/safe/html -[`seq`]: /functions/collections/seq [`Site`]: /methods/site/ [`template`]: /functions/go-template/template/ [`Title`]: /methods/page/title