Merge commit 'e509cac533600cf4fa8382c9cdab78ddd82db688'

This commit is contained in:
Bjørn Erik Pedersen
2023-10-20 09:43:56 +02:00
298 changed files with 4568 additions and 1991 deletions

View File

@@ -0,0 +1,55 @@
---
title: len
description: Returns the length of a string, slice, map, or collection.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: []
returnType: int
signatures: [len INPUT]
relatedFunctions:
- len
- strings.Count
- strings.CountRunes
- strings.CountWords
- strings.RuneCount
aliases: [/functions/len]
---
{{% readfile file="/functions/_common/go-template-functions.md" %}}
With a string:
```go-html-template
{{ "ab" | len }} → 2
{{ "" | len }} → 0
```
With a slice:
```go-html-template
{{ slice "a" "b" | len }} → 2
{{ slice | len }} → 0
```
With a map:
```go-html-template
{{ dict "a" 1 "b" 2 | len }} → 2
{{ dict | len }} → 0
```
With a collection:
```go-html-template
{{ site.RegularPages | len }} → 42
```
You may also determine the number of pages in a collection with:
```go-html-template
{{ site.RegularPages.Len }} → 42
```

View File

@@ -0,0 +1,159 @@
---
title: range
description: Iterates over slices, maps, and page collections.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: []
returnType:
signatures: [range COLLECTION]
relatedFunctions:
- with
- range
aliases: [/functions/range]
toc: true
---
{{% readfile file="/functions/_common/go-template-functions.md" %}}
## Slices
Template:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
<p>{{ . }}</p>
{{ end }}
```
Result:
```html
<p>foo</p>
<p>bar</p>
<p>baz</p>
```
Template:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $v := $s }}
<p>{{ $v }}</p>
{{ end }}
```
Result:
```html
<p>foo</p>
<p>bar</p>
<p>baz</p>
```
Template:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $k, $v := $s }}
<p>{{ $k }}: {{ $v }}</p>
{{ end }}
```
Result:
```html
<p>0: foo</p>
<p>1: bar</p>
<p>2: baz</p>
```
## Maps
Template:
```go-html-template
{{ $m := slice
(dict "name" "John" "age" 30)
(dict "name" "Will" "age" 28)
(dict "name" "Joey" "age" 24)
}}
{{ range $m }}
<p>{{ .name }} is {{ .age }}</p>
{{ end }}
```
Result:
```html
<p>John is 30</p>
<p>Will is 28</p>
<p>Joey is 24</p>
```
## Page collections
Template:
```go-html-template
{{ range where site.RegularPages "Type" "articles" }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
```
Result:
```html
<h2><a href="/articles/article-3/">Article 3</a></h2>
<h2><a href="/articles/article-2/">Article 2</a></h2>
<h2><a href="/articles/article-1/">Article 1</a></h2>
```
## Break
Use the `break` statement to stop the innermost iteration and bypass all remaining iterations.
Template:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
{{ if eq . "bar" }}
{{ break }}
{{ end }}
<p>{{ . }}</p>
{{ end }}
```
Result:
```html
<p>foo</p>
```
## Continue
Use the `continue` statement to stop the innermost iteration and continue to the next iteration.
Template:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
{{ if eq . "bar" }}
{{ continue }}
{{ end }}
<p>{{ . }}</p>
{{ end }}
```
Result:
```html
<p>foo</p>
<p>baz</p>
```

View File

@@ -0,0 +1,32 @@
---
title: urlquery
description: Returns the escaped value of the textual representation of its arguments in a form suitable for embedding in a URL query.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: []
returnType: string
signatures: ['urlquery INPUT [INPUT]...']
relatedFunctions:
- collections.Querify
- urlquery
aliases: [/functions/urlquery]
---
{{% readfile file="/functions/_common/go-template-functions.md" %}}
This template code:
```go-html-template
{{ $u := urlquery "https://" "example.com" | safeURL }}
<a href="https://example.org?url={{ $u }}">Link</a>
```
Is rendered to:
```html
<a href="https://example.org?url=https%3A%2F%2Fexample.com">Link</a>
```

View File

@@ -0,0 +1,35 @@
---
title: with
description: Rebinds the context (`.`) within its scope and skips the block if the variable is absent or empty.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: []
returnType: any
signatures: [with PIPELINE]
relatedFunctions:
- with
- range
aliases: [/functions/with]
---
{{% readfile file="/functions/_common/go-template-functions.md" %}}
An alternative way of writing an `if` statement and then referencing the same value is to use `with` instead. `with` rebinds the context (`.`) within its scope and skips the block if the variable is absent, unset or empty.
The set of *empty* values is defined by [the Go templates package](https://golang.org/pkg/text/template/). Empty values include `false`, the number zero, and the empty string.
If you want to render a block if an index or key is present in a slice, array, channel or map, regardless of whether the value is empty, you should use [`isset`](/functions/collections/isset) instead.
The following example checks for a [user-defined site variable](/variables/site/) called `twitteruser`. If the key-value is not set, the following will render nothing:
{{< code file="layouts/partials/twitter.html" >}}
{{ with .Site.Params.twitteruser }}<span class="twitter">
<a href="https://twitter.com/{{ . }}" rel="author">
<img src="/images/twitter.png" width="48" height="48" title="Twitter: {{ . }}"
alt="Twitter"></a>
</span>{{ end }}
{{< /code >}}