[SOLVED] {{ isset }} How to check variable is empty?


#1

Well, following on from my idiotic ramblings about {{ isset }} in another thread, I’ve now pinned down the last of my problems. Thought I’d start a new thread for it so I can make the title a bit more specific:

Basically, I have the following code which assigns an icon to a post if one has been defined in the frontmatter, or assigns it the default icon if none has been set.

  					{{ if (isset .Params "mdrposticon") }}
  					<img src="{{.Params.mdrposticon }}" />
  					{{"<!--  Posticon. Otherwise show default //-->" | safeHTML}}
  					{{ else }}
  					<img src="/icons/default.png" />
  					{{ end }}
  				</div>```

The problem is that; if I don't include the  the *mdrposticon* variable in the frontmatter at all, everything works fine and my posts get assigned the default fallback icon. However, if I include the *mdrposticon* variable line in the frontmatter but leave it empty. ie. ``mdrposticon=""`` Hugo is treating the variable as having been set. and is using  ```src=""``` as the attribute in the *img* tag.

What I want is to be able to test for the variable having been left empty and treat that as if it hasn't been defined. [pseudo-code]:

```if mdrposticon is not set OR  mdrposticon = ""
then show default icon
else
show specified icon```

Unfortunately I'm not having much joy in getting my head round golang template code for this. I've tried:

````{{ if or (not (isset .Params "mdrposticon")) (.Params "mdrposticon" eq "") }}````

and

```{{ if not (isset .Params "mdrposticon") | or (.Params "mdrposticon" "") }}```

and 

```{{ if not (isset .Params "mdrposticon") | or (.Params "mdrposticon" == "") }}```

etc. etc. But all to no avail.

Can someone put me out of my misery and tell me the correct syntax to use?

#2

Well, I think I’ve cracked it after pretty much trying every combination I could think of until one worked. So for future reference, here’s what did the business:

					{{"<!--  Posticon. If blank or not set, show default //-->" | safeHTML}}
  					{{ if (not (isset .Params "mdrposticon")) | or (eq .Params.mdrposticon "") }}
  					<img src="/icons/default.png" />
					{{"<!--  Posticon. Otherwise show defined icon //-->" | safeHTML}}
  					{{ else }}
					<img src="{{.Params.mdrposticon }}" />
  					{{ end }}
  				</div>```

What a confusing template syntax!

#3

Really appreciate this @stiobhart. I was working on a task to figure out how to pare down my frontmatter, and was trying to figure out the logic for negation. I don’t want to put showpaging: true etc, but rather would add a line only when I want to turn off the default. This post really helped.

This is what I came up with for one of the params hidepaging.

{{ if (not (isset .Params "hidepaging")) }}
<div class="paging">
  {{ if .Prev }}
  <div class="paging-newer">
     <span class="bold">Next Newer: </span>
     <a class="paging-link" href="{{ .Prev.RelPermalink }}">{{ .Prev.Title }}</a>
  </div>
  {{ end }}
  {{ if .Next }}
  <div class="paging-older">
     <span class="bold">Next Older: </span>
     <a class="paging-link" href="{{ .Next.RelPermalink }}">{{ .Next.Title  }}</a>
  </div>
  {{ end }}
</div>
{{ end }}

This code will display the block if that param is not present.


#4

Good examples, thanks. I just worked out if it’s Data files (where it’s not .Params), this is needs to be done without isset:

For variable name set in a Data file, I expected:

{{ if ( isset .name ) }}

to be the way to do it. But in fact it needed:

{{ if .name }}

May be obvious to some, useful to others


#5

Post #4 is the one that worked for me, and it worked with .Params, whereas for some reason the above examples with isset did not work. I had a variable in the front matter called “section_number” and it was set to "" in some cases and e.g. “1.2” in others. Using the following code, it was showing “: Title” instead of just “Title” using isset. Once I switched it then stopped showing the colon.

  {{ if .Params.section_number }}
    <li><a class="section-jump" href="#{{.Params.weight}}">{{ .Params.section_number }}: {{ .Title }}</a></li>
  {{ else }}
    <li><a class="section-jump" href="#{{.Params.weight}}">{{ .Title }}</a></li>
{{ end }}

#6

Thank you, kind gentleman. May the good winds carry you to success.


#7

I just noticed that this topic (despite its age) is the 2nd most referred topic in the Forum and I would like to point out for posterity, that there is a better Hugo function to check whether a variable is set or not.

with skips the code block if the variable is absent from the template.

When using isset it is not as straightforward to skip a block (as the above replies show) and as a result you may encounter Hugo errors.

So I highly recommend that you use with in your templates.

In most cases it’s simpler and more elegant.