# Check empty front matter field

**URL:** <https://discourse.gohugo.io/t/check-empty-front-matter-field/42172>\
**Category:** support\
**Created:** [January 1, 2023, 11:07am UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172 "2023-01-01T11:07:02Z")\
**Posts on this page:** 15\
**Page:** 2

<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:** [January 3, 2023, 7:05pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/21 "2023-01-03T19:05:07Z")

</div>

The following should work (but judging by the history of this thread it does not):

1. Use `with` it will do the job of checking if the info is set. Now if it’s an empty string or false value, it will skip. It also shift context, so on success the `.` now contains your `.Params.info`
2. The second test ensure that `.Params.info` which now is not in the `.` does not return an whitespace which `with` will consider truthy.

```auto
{{ with .Params.info }}
  {{ if ne . " " }}
    {{T "Info"}}: {{ . }}
  {{end}}
{{ end }}

```

If the above does not work I’m out of option without seeing the data you’re trying to process and your repo.

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [January 3, 2023, 7:11pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/22 "2023-01-03T19:11:06Z")

</div>

> [@regis](#):
>
> Use `with` it will do the job of checking if the info is set. Now if it’s an empty string or false value, it will skip.

It failed. Not sure why.

> [@regis](#):
>
> The second test ensure that `.Params.info` which now is not in the `.` does not return an whitespace which `with` will consider truthy.

This worked! I will just now replicate it for the other fields.

---

<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:** [January 3, 2023, 7:25pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/23 "2023-01-03T19:25:32Z")

</div>

> [@steppa](#):
>
> It failed. Not sure why.

`with` will return if the value is a lone whitespace(`" "`), it is a truthy value for `with`.

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [January 3, 2023, 7:31pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/24 "2023-01-03T19:31:48Z")

</div>

There is no white space in the data. So, that might be why it fails.

---

<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:** [January 3, 2023, 7:50pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/25 "2023-01-03T19:50:06Z")

</div>

I’m not sure what you mean by “it fails”?  
If there is no whitespace in the data, then you don’t need `if ne " "`.

`with` should be the only test needed. As it will skip if

1. The key is not set
2. The key is set but has an empty string for value.

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [January 4, 2023, 8:23am UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/26 "2023-01-04T08:23:25Z")

</div>

> [@regis](#):
>
> if the value is a lone whitespace(`" "`)

This is the result of `with` [Screenshot by Lightshot](https://prnt.sc/PIp2kpKVaq4r) (see location and the empty space).

---

<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:** [January 4, 2023, 5:13pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/27 "2023-01-04T17:13:09Z")

</div>

Is this problem resolved?

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [January 4, 2023, 6:51pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/28 "2023-01-04T18:51:32Z")

</div>

> [@regis](#):
>
> ```auto
> {{ with .Params.info }}
> {{ if ne . " " }}
> {{T "Info"}}: {{ . }}
> {{end}}
> {{ end }}
> 
> ```

@jmooring this did, unless there is a more ‘elegant’ solution.

---

<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:** [January 4, 2023, 6:53pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/29 "2023-01-04T18:53:04Z")

</div>

So there _is_ a space, not an empty string, in the value?

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [January 4, 2023, 6:54pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/30 "2023-01-04T18:54:49Z")

</div>

The remote data has no white space in blank values. It is `""` not `" "`

---

<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:** [January 4, 2023, 6:55pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/31 "2023-01-04T18:55:12Z")

</div>

Then why are you testing for a space?

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [January 4, 2023, 6:58pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/32 "2023-01-04T18:58:22Z")

</div>

I didn’t know the two are different. I just wanted to avoid the empty values from appearing in my content unless they have values in them. E.g If `location:""` don’t show vs if `location: Tokyo` then show…

---

<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:** [January 4, 2023, 7:03pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/33 "2023-01-04T19:03:54Z")

</div>

> [@steppa](#):
>
> I didn’t know the two are different.

One is truthy, the other is not.

If you’re sure you don’t have a space in the value, this is sufficient:

```plaintext
{{ with .Params.info }}
  {{ T "info" }}: {{ . }}
{{ end }}

```

If it _might_ have a space in the value:

```plaintext
{{ with (trim .Params.info " ") }}
  {{ T "info" }}: {{ . }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![steppa](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@steppa](https://discourse.gohugo.io/u/steppa)\
**Post date:** [January 4, 2023, 9:12pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/34 "2023-01-04T21:12:59Z")

</div>

> [@jmooring](#):
>
> ```auto
> {{ with (trim .Params.info " ") }}
> {{ T "info" }}: {{ . }}
> {{ end }}
> 
> ```

This worked!

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [January 6, 2023, 9:13pm UTC](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172/35 "2023-01-06T21:13:29Z")

</div>

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

[Previous page](https://discourse.gohugo.io/t/check-empty-front-matter-field/42172.md?page=1)
