Hugo: isset doesn't always seem to work

Created on 23 Feb 2017  Â·  8Comments  Â·  Source: gohugoio/hugo

Hello,

I am filing this bug report based on the discussion here.

Hugo version: Latest from master as of today.

I use the below snippet in my template, and it works.

{{ if isset .Site.Params "isso" }}
    <div class="comments clear-float">
        <h1>Comments</h1>
        {{ partial "isso" . }}
    </div>
{{ else if .Site.DisqusShortname }}
    <div class="comments clear-float">
        <h1>Comments</h1>
        {{ partial "disqus" . }}
    </div>
{{ end }}

But..

instead of {{ else if .Site.DisqusShortname }}, if I use

<!-- snip -->
{{ else if isset .Site "DisqusShortname" }}
<!-- snip -->

or

<!-- snip -->
{{ else if isset $.Site "DisqusShortname" }}
<!-- snip -->

then it does not work.. that if condition always evaluates to false even when I have DisqusShortname = "foo" in my config.toml.

But it worked fine in

{{ if isset .Site.Params "isso" }}

I tested the above isso condition getting true by putting the below in my config:

[Params]
  [Params.isso]
    server = "localhost:1234"

So it is hard to tell when isset is working correctly and when it's not.

All 8 comments

Ref. doc:

isset

Returns true if the parameter is set. Takes either a slice, array or channel and an index or a map and a key as input.

Which matches your examples perfectly.

Consider using with.

Sorry for a 101 question.. but I still do not follow.

Takes either a slice, array or channel and an index or a map and a key as input.

So looks like isset takes 2 arguments:

  1. slice, array or channel-and-index or a map and
  2. a key

{{ if isset .Site.Params "isso" }} works..

So,

  • Probably .Site.Params is one of: slice, array or channel-and-index or map.
  • "isso" is the key.

In the case of {{ else if isset .Site "DisqusShortname" }},

  • If .Site is none of: slice, array,channel-and-index, map, then what is it?
  • Isn't "DisqusShortname" a key in .Site?

Please use http://discuss.gohugo.io/ for questions/troubleshooting.

I already started a thread there, and that led me to filing this issue: https://discuss.gohugo.io/t/solved-how-to-check-if-a-parameter-exists-in-config-toml-not-under-params/5569/12

Good.

Its never work, wasting my time.

For those suffering with this idiotic behaviour with isset or other condition structures in general and for those those coming here from a Google Search hoping to find an answer, thinking they're the responsible, that they have created the condition statement wrong, instead of keep searching for countless hours try to restart your server or regenerate the HTML files again

I have this problem myself in a website with videos that I wanted to provide to the search engines as much information as possible about the video files BUT I wouldn't simply throw N Parameters in my FrontMatters without any sort of organization or logical structure so I did this:

details:
  season: 2
  episode:
    this: 1
    abs: 11
  audio: English (AAC)
  subtitles:
  - name: "Full Language Name 1"
    code: "ISO Code"
  - name: "Full Language Name 2"
    code: "ISO Code"
  duration:
    display: 19min30s # For HTML markup
    iso: PT19M30S      # For OpenGraph <meta> Tags
    seconds: 1170       # For Facebook OpenGraph video.duration <_<
  height: 720
  width: 1280
  quality: HD 720p (h.264 High Profile @ L3.1)
  mirrors:
  - name: "Site Name #1"
    hash: "site1-internal-ID"
    url: "http://www.site1.com/video.mp4"
    uploadDate: 2018-05-24T11:21:36-03:00
  - name: "Site Name #2"
    hash: "site2-internal-ID"
    url: "http://www.site2.com/video.mp4"
    uploadDate: 2018-05-24T11:21:36-03:00

And then I created the simplest condition I imagined:

{{ if (ne .Params.details.mirrors nil) }}
    Shows a lot of information with the details
{{ else }}
    Shows an error message saying that no mirrors are available
{{ end }}

Expecting the details markup for a Content File that has a mirror entry under details in my FrontMatter and the error message if no mirrors exist and therefore there are no such entries.

But no matter what I couldn't have the expected TRUE and FALSE distinction, be it with isset, with and neither the way I currently do, after stressing myself without support with the weirdnesses of isset, that's testing for nil, something I didn't find in official Hugo docs only in Go docs (!).

I was about to remove the condition, taking the damage of fooling my visitors, when I accidentally regenerated my HTML files from scratch, for the first time in that working day — meaning without starting the server that day and thus the wrong behaviour never "triggered — and everything worked as expected.

It seems that conditions work when they want to work, simple like that. Now every single time I update contents and have to regenerate the files I must manually check each entry that could potentially have a wrong result (in my case, usually Paginated Pages — "lists") and if needed I had to delete everything and generate everything again.

Ah! This post IS NOT a "tall order". Just read it, it's a SOLUTION for a problem still present in the latest version.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arikroc picture arikroc  Â·  3Comments

crash-dive picture crash-dive  Â·  3Comments

vielmetti picture vielmetti  Â·  3Comments

ianbrandt picture ianbrandt  Â·  3Comments

tjamet picture tjamet  Â·  3Comments