# Check if empty front matter array

**URL:** https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738
**Category:** support
**Created:** [January 30, 2019, 6:15pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738 "2019-01-30T18:15:36Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![pancake](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pancake](https://discourse.gohugo.io/u/pancake)
#### Post date: [January 30, 2019, 6:15pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/1 "2019-01-30T18:15:36Z")

</div>

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`

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [January 30, 2019, 6:22pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/2 "2019-01-30T18:22:50Z")

</div>

> [@pancake](#):
>
> {{with ne .Params.product nil}}

I’m pretty sure that:

```auto
{{ with .Params.product }}

```

Should work.

---

<div class="post-metadata">

### Author: ![pancake](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pancake](https://discourse.gohugo.io/u/pancake)
#### Post date: [January 30, 2019, 6:28pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/3 "2019-01-30T18:28:54Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [January 30, 2019, 6:29pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/4 "2019-01-30T18:29:39Z")

</div>

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

```nohighlight
{{ $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 }}

```

---

<div class="post-metadata">

### Author: ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)
#### Post date: [May 29, 2019, 4:48pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/5 "2019-05-29T16:48:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [May 29, 2019, 5:16pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/6 "2019-05-29T17:16:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)
#### Post date: [May 29, 2019, 7:29pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/7 "2019-05-29T19:29:06Z")

</div>

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.`

---

<div class="post-metadata">

### Author: ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)
#### Post date: [May 30, 2019, 8:48am UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/8 "2019-05-30T08:48:05Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)
#### Post date: [May 30, 2019, 8:56am UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/9 "2019-05-30T08:56:06Z")

</div>

```auto
{{ .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.

---

<div class="post-metadata">

### Author: ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)
#### Post date: [May 30, 2019, 10:35am UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/10 "2019-05-30T10:35:52Z")

</div>

And with Hugo latest goodies:

- variable overwrite
- return statement in partial which makes this test more maintainable/reusuable

```auto
{{/* layouts/partials/func/HasValues.html */}}

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

{{ return $return }}

```

```auto
{{/* layouts/_default/single.html */}}

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

```

---

<div class="post-metadata">

### Author: ![nobby](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nobby](https://discourse.gohugo.io/u/nobby)
#### Post date: [May 31, 2019, 4:18pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/11 "2019-05-31T16:18:30Z")

</div>

Super helpful. Thanks @davidsneighbour and @regis

---

<div class="post-metadata">

### Author: ![codingforfun](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@codingforfun](https://discourse.gohugo.io/u/codingforfun)
#### Post date: [September 3, 2021, 7:58am UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/12 "2021-09-03T07:58:52Z")

</div>

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.

```auto
{{ if partial "functions/greater-than" (dict "map" .Params.section "gt" 1) }}
Execute code here
{{ end }}

```

```auto
{{ $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.

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [September 3, 2021, 1:14pm UTC](https://discourse.gohugo.io/t/check-if-empty-front-matter-array/16738/13 "2021-09-03T13:14:03Z")

</div>


