Skip to content

Commit

Permalink
Update function signatures
Browse files Browse the repository at this point in the history
Co-authored-by: irkode <irkode@rikode.de>
  • Loading branch information
jmooring and irkode committed May 11, 2024
1 parent 463adcf commit fb76580
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
35 changes: 10 additions & 25 deletions content/en/functions/collections/Dictionary.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
---
title: collections.Dictionary
description: Creates a map from a list of key and value pairs.
description: Returns a map composed of the given key-value pairs.
categories: []
keywords: []
action:
aliases: [dict]
related:
- functions/collections/Slice
returnType: mapany
signatures: ['collections.Dictionary KEY VALUE [VALUE...]']
signatures: ['collections.Dictionary [VALUE...]']
aliases: [/functions/dict]
---

Specify the key-value pairs as individual arguments:

```go-html-template
{{ $m := dict "a" 1 "b" 2 }}
```
Expand All @@ -25,6 +27,12 @@ The above produces this data structure:
}
```

To create an empty map:

```go-html-template
{{ $m := dict }}
```


Note that the `key` can be either a `string` or a `string slice`. The latter is useful to create a deeply nested structure, e.g.:

Expand All @@ -43,26 +51,3 @@ The above produces this data structure:
}
}
```

## Pass values to a partial template

The partial below creates an SVG and expects `fill`, `height` and `width` from the caller:

### Partial definition

{{< code file=layouts/partials/svgs/external-links.svg >}}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
fill="{{ .fill }}" width="{{ .width }}" height="{{ .height }}" viewBox="0 0 32 32" aria-label="External Link">
<path d="M25.152 16.576v5.696q0 2.144-1.504 3.648t-3.648 1.504h-14.848q-2.144 0-3.648-1.504t-1.504-3.648v-14.848q0-2.112 1.504-3.616t3.648-1.536h12.576q0.224 0 0.384 0.16t0.16 0.416v1.152q0 0.256-0.16 0.416t-0.384 0.16h-12.576q-1.184 0-2.016 0.832t-0.864 2.016v14.848q0 1.184 0.864 2.016t2.016 0.864h14.848q1.184 0 2.016-0.864t0.832-2.016v-5.696q0-0.256 0.16-0.416t0.416-0.16h1.152q0.256 0 0.416 0.16t0.16 0.416zM32 1.152v9.12q0 0.48-0.352 0.8t-0.8 0.352-0.8-0.352l-3.136-3.136-11.648 11.648q-0.16 0.192-0.416 0.192t-0.384-0.192l-2.048-2.048q-0.192-0.16-0.192-0.384t0.192-0.416l11.648-11.648-3.136-3.136q-0.352-0.352-0.352-0.8t0.352-0.8 0.8-0.352h9.12q0.48 0 0.8 0.352t0.352 0.8z"></path>
</svg>
{{< /code >}}

### Partial call

The `fill`, `height` and `width` values can be stored in one object with `dict` and passed to the partial:

{{< code file=layouts/_default/list.html >}}
{{ partial "svgs/external-links.svg" (dict "fill" "#01589B" "width" 10 "height" 20 ) }}
{{< /code >}}

[partials]: /templates/partials/
2 changes: 1 addition & 1 deletion content/en/functions/collections/KeyVals.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ action:
related:
- methods/pages/Related
returnType: types.KeyValues
signatures: [collections.KeyVals KEY VALUES...]
signatures: [collections.KeyVals KEY VALUE...]
aliases: [/functions/keyvals]
---

Expand Down
25 changes: 15 additions & 10 deletions content/en/functions/collections/Querify.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: collections.Querify
description: Takes a set or slice of key-value pairs and returns a query string to be appended to URLs.
description: Returns a URL query string composed of the given key-value pairs.
categories: []
keywords: []
action:
Expand All @@ -9,24 +9,29 @@ action:
- functions/go-template/urlquery.md
returnType: string
signatures:
- collections.Querify VALUE [VALUE...]
- collections.Querify COLLECTION
- collections.Querify [VALUE...]
aliases: [/functions/querify]
---

`querify` takes a set or slice of key-value pairs and returns a [query string](https://en.wikipedia.org/wiki/Query_string) that can be appended to a URL.
Specify the key-value pairs as individual arguments, or as a slice. The following are equivalent:

The following examples create a link to a search results page on Google.

```go-html-template
<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>
{{ collections.Querify "a" 1 "b" 2 }}
{{ collections.Querify (slice "a" 1 "b" 2) }}
```

To append a query string to a URL:

```go-html-template
{{ $qs := collections.Querify "a" 1 "b" 2 }}
{{ $href := printf "https://example.org?%s" $qs }}
{{ $qs := slice "q" "test" "page" 3 }}
<a href="https://www.google.com?{{ (querify $qs) | safeURL }}">Search</a>
<a href="{{ $href }}">Link</a>
```

Both of these examples render the following HTML:
Hugo renders this to:

```html
<a href="https://www.google.com?page=3&q=test">Search</a>
<a href="https://example.org?a=1&amp;b=2">Link</a>
```
10 changes: 8 additions & 2 deletions content/en/functions/collections/Slice.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
---
title: collections.Slice
description: Creates a slice of all passed arguments.
description: Returns a slice composed of the given values.
categories: []
keywords: []
action:
aliases: [slice]
related:
- functions/collections/Dictionary
returnType: any
signatures: [collections.Slice ITEM...]
signatures: ['collections.Slice [VALUE...]']
aliases: [/functions/slice]
---

```go-html-template
{{ $s := slice "a" "b" "c" }}
{{ $s }} → [a b c]
```

To create an empty slice:

```go-html-template
{{ $s := slice }}
```

0 comments on commit fb76580

Please sign in to comment.