Checking for empty values and values with ""

Trying to only show items that currently have a value, I’ve noticed my .md files are saving some items with no values with a line break and “” in some instances. I do not want to show empty items, or items with a “”.

- template: people
  list:
- template: regions
  list:
    - ""

I’ve tried the following code which works for not displaying items without any content, but still shows items with “”.

{{ if ( .list ) and ( .list "!=" "") }}

 {{ range .list }}
 <p> {{ . }}</p>
 {{ end }}

 {{ else }}
  <p> no items found </p>
 {{ end }}
{{ end }}

Can’t figure out what I am missing.

One option is to remove all those empty strings from your markdown files

I originally was hoping to do this, but the .md files are generated from an app, and there is a high volume of files being generated, so manually going in to delete the “” would not be an option.

First, your if-statement is incorrect. In Hugo templates the operator comes first.

{{ if and .list (ne .list "") }}

However, I’d recommend using with here:

{{ with .list }}
 {{ range . }}
  <p>{{ . }}</p>
 {{ end }}
{{ else }}
 <p>no items found</p>
{{ end }}

The inner {{.}} within the range may need its own {{ with }} block, too.

1 Like