hugo/docs/content/en/functions/collections/Dictionary.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

2.1 KiB

title description categories keywords action aliases
collections.Dictionary Creates a map from a list of key and value pairs.
aliases related returnType signatures
dict
functions/collections/Slice
mapany
collections.Dictionary KEY VALUE [VALUE...]
/functions/dict
{{ $m := dict "a" 1 "b" 2 }}

The above produces this data structure:

{
  "a": 1,
  "b": 2
}

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.:

{{ $m := dict (slice "a" "b" "c") "value" }}

The above produces this data structure:

{
  "a": {
    "b": {
      "c": "value"
    }
  }
}

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 >}} {{< /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 >}}