HUGO
Menu
GitHub 86504 stars Mastodon

collections.Querify

Returns a URL query string from the given map, slice, or sequence of key-value pairs.

Syntax

collections.Querify MAP|SLICE|KEY VALUE...

Returns

string

Alias

querify

Specify the key-value pairs as a map, a slice, or a sequence of scalar values. For example, the following are equivalent:

{{ collections.Querify (dict "a" 1 "b" 2) }}
{{ collections.Querify (slice "a" 1 "b" 2) }}
{{ collections.Querify "a" 1 "b" 2 }}

To append a query string to a URL:

{{ $qs := collections.Querify (dict "a" 1 "b" 2) }}
{{ $href := printf "https://example.org?%s" $qs }}

<a href="{{ $href }}">Link</a>

Hugo renders this to:

<a href="https://example.org?a=1&amp;b=2">Link</a>

You can also pass in a map from your site configuration or front matter. For example:

---
params:
  query:
    a: 1
    b: 2
title: Example
---
+++
title = 'Example'
[params]
  [params.query]
    a = 1
    b = 2
+++
{
   "params": {
      "query": {
         "a": 1,
         "b": 2
      }
   },
   "title": "Example"
}
{{ collections.Querify .Params.query }}