| 1 |
--- |
| 2 |
title: collections.Querify |
| 3 |
description: Returns a URL query string from the given map, slice, or sequence of key-value pairs. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [querify] |
| 9 |
returnType: string |
| 10 |
signatures: [collections.Querify MAP|SLICE|KEY VALUE...] |
| 11 |
aliases: [/functions/querify] |
| 12 |
--- |
| 13 |
|
| 14 |
Specify the key-value pairs as a map, a slice, or a sequence of scalar values. For example, the following are equivalent: |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ collections.Querify (dict "a" 1 "b" 2) }} |
| 18 |
{{ collections.Querify (slice "a" 1 "b" 2) }} |
| 19 |
{{ collections.Querify "a" 1 "b" 2 }} |
| 20 |
``` |
| 21 |
|
| 22 |
To append a query string to a URL: |
| 23 |
|
| 24 |
```go-html-template |
| 25 |
{{ $qs := collections.Querify (dict "a" 1 "b" 2) }} |
| 26 |
{{ $href := printf "https://example.org?%s" $qs }} |
| 27 |
|
| 28 |
<a href="{{ $href }}">Link</a> |
| 29 |
``` |
| 30 |
|
| 31 |
Hugo renders this to: |
| 32 |
|
| 33 |
```html |
| 34 |
<a href="https://example.org?a=1&b=2">Link</a> |
| 35 |
``` |
| 36 |
|
| 37 |
You can also pass in a map from your project configuration or front matter. For example: |
| 38 |
|
| 39 |
{{< code-toggle file=content/example.md fm=true >}} |
| 40 |
title = 'Example' |
| 41 |
[params.query] |
| 42 |
a = 1 |
| 43 |
b = 2 |
| 44 |
{{< /code-toggle >}} |
| 45 |
|
| 46 |
```go-html-template |
| 47 |
{{ collections.Querify .Params.query }} |
| 48 |
``` |