If I do {{with ne .Params.product nil}} gives true when there are values listed in the front matter, which is correct, but this also prevents me from doing {{.location}} etc to get the values.
Error message: can't evaluate field Params in type bool
Found this via search. This is a problem I’m now encountering.
is there a better way to check if all the values in an inline table are empty strings? @zwbetz solution works but becomes problematic when you’re dealing with lots of keys.
The use case for this is that I only want to create a <section> if there is at least one value that is not empty under the inline table. If not, the <section> does not appear.
Do you know the keys under product or are they arbitrary? If they are fixed I would create a shortcode that is checking each value and then put the section inside of the shortcode to be returned only if the check true for keys containing content. This way you don’t repeat it over and over.
If you don’t know the keys then I suggest you check if the process of creating them can be changed. If done by hand I would leave the product key out and use a simple with check. If a script is doing it - see if you can add a way to see if the map has empty values.
Last thought: I have no knowledge of using the Scratch function, but what about iterating through your map, concatenate the values to a string and at the end check if the string has length 0?
This is incredibly useful and I used it as a starting point for my problem. In my case I want to know if my map size is greater than a specific number.
{{ if partial "functions/greater-than" (dict "map" .Params.section "gt" 1) }}
Execute code here
{{ end }}
{{ $counter := 0 }}
{{ $return := false }}
{{ with .map }}
{{ range $key, $value := . }}
{{ with $value }}
{{ $counter = add $counter 1 }}
{{ end }}
{{ end }}
{{ end }}
{{ if gt $counter .gt }}{{ $return = true }}{{ end }}
{{ return $return }}