× Search About Posts Code Music Links
Blank Try
experiment
lots
learn
more

Arrays

Updated on
Last site update: 8 May 2024

Arrays in the frontmatter (YAML) can be written as:

1fish: ["shark", "herring", "squid", "chair"]

or

1fish:
2- shark
3- herring
4- squid
5- chair

To access an item of an array from Hugo template use with and index:

1{{ with .Params.fish }}
2  {{ index . 1 }}
3{{ end }}

You can also have arrays of arrays. For example:

1watercreatures:
2 - ["shark", "herring", "squid", "chair"]
3 - ["starfish", "sand dollar", "nautilous", "anemone"]
4 - ["orca", "dolphin", "sperm whale", "manatee"]

So how do we access these?

1{{ with .Page.Params.watercreatures }}
2 {{ index . 1 2 }}
3{{ end }}

The first index, 1, selects the second array. The second number, 2, selects the third item of the array.

How do we get a list of all of them? in order?

Using the arrays.html shortcode:

114 110 108
  • [shark herring squid chair]
  • [starfish sand dollar nautilous anemone]
  • [orca dolphin sperm whale manatee]