Check for key in dict?

I tried:

{{ if in $current .Params.bom }}

where $current is part-2 and .Params.bom is defined in the front matter as:

bom: { 'part-1' : 17, 'part-2' : 24 }

but that does not seem to evaluate to true. Neither if in .Params.bom $current.

Also tried .Params.bom.$current but that is a syntax errror.

What’s the right way of doing this?

{{ $current := "part-2" }}
{{ with .Params.bom }}
  {{ if index . $current }}
    ...
    ...
  {{ end }}
{{ end }}
1 Like

The syntax of in is by the way: in SET ITEM, not in ITEM SET. So your initial attempt might have worked too with exchanged parameters. But @jmooring has a better solution, because it checks if both, the set and the item are existing first.

1 Like

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