Check if empty front matter array

With the following front matter:

[product]
  location = ""
  price = ""
  supplier = ""
  unit_text = ""

How do I check if .Params.product is empty?

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

I’m pretty sure that:

{{ with .Params.product }}

Should work.

Doesn’t work.

With empty front matter:

{{with .Params.product}}
true
{{end}}

Shows true which it shouldn’t do since there are no values. At least this is the behavior I’m looking for.

{{.Params.product}} gives map[location: price: supplier: unit_text:]

So I guess {{.Params.product}} is not nil as it has keys, so perhaps my question should be: how do you check for empty values?

It sounds like you want an empty string to equate as empty, vs nil as empty. So you could do something like this, although it’s convoluted

{{ $productIsEmpty := false }}

{{ with .Params.product }}
  {{ if and (eq .location "") (and (eq .price "") (and (eq .supplier "") (eq .unit_text ""))) }}
    {{ $productIsEmpty = true }}
  {{ end }}
{{ end }}

Product is empty = {{ $productIsEmpty }}

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.

I don’t know of a better way. But others on here may

Thanks. For my use case, it seems like creating an additional key may be a better approach. Using the OP’s example something like:

[product]
  product_meta = false
  location = ""
  price = ""
  supplier = ""
  unit_text = ""

And querying product_meta.

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?

{{ .Scratch.Set "values" "" }}
{{ range $key, $value := . }}
    {{ .Scratch.Add "values" $value }}
{{ end }}
{{ if gt (len (.Scratch.Get "values")) 0 }}
// your section goes here
{{ end }}

Something like that. I am not sure if the syntax of the IF query is correct.

And with Hugo latest goodies:

  • variable overwrite
  • return statement in partial which makes this test more maintainable/reusuable
{{/* layouts/partials/func/HasValues.html */}}

{{ $return := false }}
{{ with . }}
  {{ range $key, $value := . }}
    {{ with $value }}
      {{ $return = true }}
    {{ end }}
   {{ end }}
{{ end }}

{{ return $return }}
{{/* layouts/_default/single.html */}}


 {{ with partial "func/HasValues" .Params.product }}
    🎉
 {{ end }}

7 Likes

Super helpful. Thanks @davidsneighbour and @regis

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 }}

Maybe someone else will find this helpful.