Check empty front matter field

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

2 Likes

It failed. Not sure why.

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

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

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

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.

This is the result of with Screenshot by Lightshot (see location and the empty space).

Is this problem resolved?

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

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

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

Then why are you testing for a space?

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…

One is truthy, the other is not.

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

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

If it might have a space in the value:

{{ with (trim .Params.info " ") }}
  {{ T "info" }}: {{ . }}
{{ end }}
4 Likes

This worked!

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