mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-19 11:44:42 -04:00
content: Document range-over-int introduced in v0.123.0
This commit is contained in:
parent
bc478f98f9
commit
e8e1c82aa6
@ -12,7 +12,6 @@ aliases: [/functions/shuffle]
|
|||||||
---
|
---
|
||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ collections.Shuffle (seq 1 2 3) }} → [3 1 2]
|
|
||||||
{{ collections.Shuffle (slice "a" "b" "c") }} → [b a c]
|
{{ 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 }}
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
```
|
```
|
||||||
|
|
||||||
For better performance with large collections, use the [`math.Rand`] and [`collections.Index`] functions instead:
|
|
||||||
|
|
||||||
```go-html-template
|
|
||||||
<ul>
|
|
||||||
{{ $p := site.RegularPages }}
|
|
||||||
{{ range seq 5 }}
|
|
||||||
{{ with math.Rand | mul $p.Len | math.Floor | int }}
|
|
||||||
{{ with collections.Index $p . }}
|
|
||||||
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
|
|
||||||
{{ end }}
|
|
||||||
{{ end }}
|
|
||||||
{{ end }}
|
|
||||||
</ul>
|
|
||||||
```
|
|
||||||
|
|
||||||
[`collections.Index`]:/functions/collections/indexfunction/
|
|
||||||
[`math.Rand`]: /functions/math/rand/
|
|
||||||
|
@ -18,7 +18,7 @@ The timer starts when you instantiate it, and stops when you call its `Stop` met
|
|||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ $t := debug.Timer "TestSqrt" }}
|
{{ $t := debug.Timer "TestSqrt" }}
|
||||||
{{ range seq 2000 }}
|
{{ range 2000 }}
|
||||||
{{ $f := math.Sqrt . }}
|
{{ $f := math.Sqrt . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ $t.Stop }}
|
{{ $t.Stop }}
|
||||||
|
@ -11,7 +11,7 @@ params:
|
|||||||
aliases: [/functions/range]
|
aliases: [/functions/range]
|
||||||
---
|
---
|
||||||
|
|
||||||
{{% include "/_common/functions/truthy-falsy.md" %}}
|
The collection may be a slice, a map, or an integer.
|
||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ $s := slice "foo" "bar" "baz" }}
|
{{ $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.
|
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
|
```go-html-template
|
||||||
{{ range seq 3 }}
|
{{ $s := slice "foo" "bar" "baz" }}
|
||||||
{{ .Title }}
|
{{ range $s }}
|
||||||
|
{{ .Title }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
Hugo will throw an error:
|
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]
|
> [!note]
|
||||||
> Use the `$` to get the context passed into the template.
|
> 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:
|
This template will render the page title three times:
|
||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range seq 3 }}
|
{{ $s := slice "foo" "bar" "baz" }}
|
||||||
{{ $.Title }}
|
{{ range $s }}
|
||||||
|
{{ $.Title }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
> [!note]
|
> [!note]
|
||||||
> Gaining a thorough understanding of context is critical for anyone writing template code.
|
> 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:
|
This template code:
|
||||||
|
|
||||||
@ -121,7 +127,7 @@ Is rendered to:
|
|||||||
<p>2: baz</p>
|
<p>2: baz</p>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Array or slice of maps
|
### Slice of maps
|
||||||
|
|
||||||
This template code:
|
This template code:
|
||||||
|
|
||||||
@ -144,7 +150,7 @@ Is rendered to:
|
|||||||
<p>Joey is 24</p>
|
<p>Joey is 24</p>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Array or slice of pages
|
### Slice of pages
|
||||||
|
|
||||||
This template code:
|
This template code:
|
||||||
|
|
||||||
@ -162,7 +168,7 @@ Is rendered to:
|
|||||||
<h2><a href="/articles/article-1/">Article 1</a></h2>
|
<h2><a href="/articles/article-1/">Article 1</a></h2>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Maps
|
### Maps
|
||||||
|
|
||||||
This template code:
|
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.
|
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" %}}
|
{{% include "/_common/functions/go-template/text-template.md" %}}
|
||||||
|
|
||||||
[`break`]: /functions/go-template/break/
|
[`break`]: /functions/go-template/break/
|
||||||
[`continue`]: /functions/go-template/continue/
|
[`continue`]: /functions/go-template/continue/
|
||||||
[`else`]: /functions/go-template/else/
|
[`else`]: /functions/go-template/else/
|
||||||
[`seq`]: /functions/collections/seq/
|
|
||||||
|
@ -400,14 +400,14 @@ See documentation for [`range`], [`else`], and [`end`].
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
Use the [`seq`] function to loop a specified number of times:
|
To loop a specified number of times:
|
||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ $total := 0 }}
|
{{ $s := slice }}
|
||||||
{{ range seq 4 }}
|
{{ range 3 }}
|
||||||
{{ $total = add $total . }}
|
{{ $s = $s | append . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ $total }} → 10
|
{{ $s }} → [0 1 2]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rebind context
|
### 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/
|
||||||
[`range`]: /functions/go-template/range/
|
[`range`]: /functions/go-template/range/
|
||||||
[`safeHTML`]: /functions/safe/html
|
[`safeHTML`]: /functions/safe/html
|
||||||
[`seq`]: /functions/collections/seq
|
|
||||||
[`Site`]: /methods/site/
|
[`Site`]: /methods/site/
|
||||||
[`template`]: /functions/go-template/template/
|
[`template`]: /functions/go-template/template/
|
||||||
[`Title`]: /methods/page/title
|
[`Title`]: /methods/page/title
|
||||||
|
Loading…
x
Reference in New Issue
Block a user