Can someone explain the dict function like I'm a 5-year-old?

Trying to write documentation around this function, and the truth is that I’ve never really understood it. If not examples, I’d at least appreciate the right location in terms of godocs or elsewhere? Thanks in advance.

1 Like

dict creates a map (a dictionary, hence the name), which is a collection of key/value pairs. The keys are strings you choose yourself, the values can be any object you want to associate with that key.

The main use case is to be able to send more than one object (usually the page) to the partials:

{{ partial "menu.html" (dict "page" . "custom_heading" "This is some text.") }}

The dict can take an unlimited amount of key/value pairs, but in the example above, you would then be able to do this in menu.html:

{{ .page.Title }}: {{ .custom_heading }}

The example is bad, but the above would not be possible without dict.


Thank you @bep!

@rdwatters I cracked up when I saw the title. Maybe you could tell me this. How does one destructure keys from a map using delimit?


I want to delimit the category names like:

<meta name="keywords" content="{{ delimit .Site.Taxonomies.categories ", " }}">

But that doesn’t work because delimit is working against a map. Any ideas on how to accomplish what I’m trying to do in a pithy way without using $.Scratch? See also:

Sorry @jhabdas for the delay. Rather crazy weekend.

I tried to hack around your question using slice and delimit but to no avail. Sorry I can’t be of more help.

Also, why print all the site’s taxonomy categories rather than {{.Keywords}} or a range through the page’s specific tags or keywords set in the front matter? I think this would be best set up as an front matter field with a default to a global variable, no?

Automating keyword generation as much as possible. Here’s the best I could do. I appreciate you trying. Thought it might be good for the docs. Please keep up the great work! And thanks for the help.

1 Like