Isset on object key

How can I check with isset if a object key exists?

In my yml header I have the following structure:

media:

  • featured:
  • intro:
  • etc …

In my template file I need to check if .media.intro exists. Tried the following code without any luck.

{{ if isset .Params "media.intro" }}
  {{ .. }}
{{ end }}

and

{{ if isset .Params.media "intro" }}
  {{ .. }}
{{ end }}

If you add a hyphen at the beginning of a line, you get an array that is not needed here.

media:
  featured:
  intro:

{{ if .Params.media.featured }}

1 Like

If you have several media elements, you need to do this:

media:
  -
    featured:
    intro:
  -
    featured:
    intro:
{{ if .Params.media }}
{{ range .Params.media }}
{{ if .featured }}{{ .featured }}{{ end }}
{{ if .intro }}{{ .intro }}{{ end }}
{{ end }}
{{ end }}
1 Like