Proper way to handle no elements in array?

This is failing when there’s no elements in the array:
{{ with (index (.Params.media) 0).media }} What’s the proper way to make sure I’m not getting a exception?

Please provide an example of your front matter. Thanks.

Some of my content contains:

media: [
]

There’s nothing inside of the array.

Please provide an example that has something inside of the array. Thanks.

media: [
    media: {
        source: "myImagePath",
        type: "image", 
        weight: 1, 
        alt_text: "some alt text"
    },

Thanks. I was wondering why there was a media key within a media key.

Ah sorry, it used to be “images” plural but extended it to have videos, but for lack of a better name (plural) I’m stuck with media and media.

But since I’m getting an exception on the empty media what’s the proper way to make sure it exists before I reference it?

{{ with .Params.media }}
  {{ (index . 0).media }}
{{ end }}
1 Like

FFS… Thank you… Sometimes I get too caught up in the syntax and forget basic checks.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.