Essay - Published: 2020.01.12 | dict | hugo | map |
DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)
I'm kinda new to Hugo and I'm trying to do an operation that involves inserting and iterating over a dict. I've been looking around and there seems to be some documentaiton but it's kinda fragmented all over the place. What are the basics of creating, populating, and iterating over a map?
Okay so I just went through this issue while attempting to create a stats page for my sites in (which is now live if you wanna take a look). Below I'll list what I learned about using maps in Hugo and some practical examples so that your journey might be a bit less annoying.
myMap["color"] = "red" or myMap["age"] = 25You can't really instantiate a map in Hugo - at least not an empty one. Instead you create the map as soon as you need it. This is kind of annoying if you're used to programming in other languages but does provide a useful feature of being able to check if a map is empty or not via an if statement, like this:
{{ if $myMap }}
<!-- $myMap has stuff in it! -->
{{ else }}
<!-- $myMap is empty =(( -->
{{ end }}
So to create a map, really you need to insert into a map, which I'll go over below:
Because you can't really instantiate a map, you can't easily insert directly into a map. To get around this, Hugo has a built in on Scratch (kind of this weird metaprogramming canvas / scoping thing that talking about is out of scope for this post) called SetInMap.
SetInMap's definition takes in 3 things:
It's then called as:
{{ Scratch.SetInMap "MAPNAME" "KEY" VALUE }}
Note that the MAPNAME and KEY parameters takes in strings, so you can pass in variables that hold strings instead of the strings directly. The VALUE parameter, I think, can be anything you wish though I've only tried it with ints and strings. I usually store the MAPNAME in a variable to make things easier on me and avoid typos which cause syntactical / semantic errors.
So let's go over a code example. If I want to store the key value pairs ("color", "red") and ("age", 25) I can do the following:
{{ $scratch := newScratch }}
{{ $mapName := "exampleMap" }}
{{ $scratch.SetInMap $mapName "color" "red" }}
{{ $scratch.SetInMap $mapName "age" 25 }}
Boom, we've set things in a map. That's cool, but not super useful until we can access those in some way. Let's go over that next.
There are a few ways to get our values from a map:
Scratch.Get MAPNAME - Get the mapScratch.GetSortedMapValues MAPNAME - Get the map, sorted by keysSo if I wanted to iterate over my map, I could do something like this (assuming I had already insterted things into it):
{{ range $k, $v := ($scratch.Get $mapName)}}
Key: {{ $k }}
Value: {{ $v }}
{{ end }}
That's it! If there's more you want to know about maps and their usage in Hugo, comment below and I'll look to add those sections here.
The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.