[SOLVED] How to check if a parameter exists in config.toml (not under [params])?

Hello,

Currently I have this in my layout:

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

disqusShortname is one of the inbuilt config.toml variable (or calling that a .Site variable is a more correct way?): https://gohugo.io/extras/comments/

So I would have liked the below to work:

{{ else if isset .Site "disqusShortname" }}

But as that does not work (neither does this {{ else if isset .Site "disqusshortname" }} – lower-cased the whole variable), I need to add this to config.toml:

[params]
  disquscomments = true

and then do:

{{ else if isset .Site.Params "disquscomments" }}

My search skills didn’t help if this was already asked and resolved in this forum. Is the {{ if isset .Site "disqusShortname" }} construct incorrect?

Found the problem… .Site.DisqusShortname is always set.

I needed to do:

{{ else if not (eq .Site.DisqusShortname "") }}

NOT TESTED

How about…

{{ with .Site.DisqusShortname}}{{.}}{{ else .Site.Params.disquscomments }}{{ end }}

But I’m not sure what you’re trying to do, so maybe use an if/else if and statement instead:

{{if .Site.Params.isso}}
     Your isso codey code coding
{{ else if and ( .Site.DisqusShortname ) ( .Site.Params.disquscomments )}}
     Your Disqus codey code coding...
{{ end}}

Second statement checks for both, although I’m not sure why you’re adding (what I assume is) a boolean to your Site params when you can just do: fill out .Site.Params.isso OR .Site.DisqusShortname. If one is filled (i.e., non-zero length on the string), it will be added to the template. So, if isso is filled, it goes to isso. If isso and disqus are filled, it goes to isso. If not isso but disqus is filled, it goes to disqus. If neither is value is filled, then it looks like you just won’t have comments :smile:

Again, not tested, and I’m pulling the casing of the Disqus param from here:

@bep had mentioned something about standardizing on making all Site params lowercase a bit ago, but I have no idea whether this has been implemented…

I am just trying to check if user wants to use Disqus. If they want to use Disqus, they would have something like below in their config.toml:

DisqusShortname = "foo"

So I assumed that if a user does not set DisqusShortname, then isset .Site.DisqusShortname will return false. But as I learn later, that isn’t the case. With the help of {{ printf "%#v" $.Site }}, I saw that it is always ‘set’ by default, but to an empty string "".

I’m pulling the casing of the Disqus param from here:

I pulled up the real casing of all .Site variables by adding {{ printf "%#v" $.Site }} for debug to one of the templates.


Thanks for the response. I was not aware that

{{ else if .Site.DisqusShortname }}

was about the same as

{{else if not (eq .Site.DisqusShortname "") }}

Now I have this:

{{ if isset .Site.Params "isso" }}
    <div class="comments clear-float">
        <h1>Comments</h1>
        {{ partial "isso" . }}
    </div>
    {{/* else if not (eq .Site.DisqusShortname "") */}}
    <!-- Below works the same way as the above empty-string equality check -->
{{ else if .Site.DisqusShortname }}
    <div class="comments clear-float">
        <h1>Comments</h1>
        {{/* template "_internal/disqus.html" . */}}
        {{ partial "disqus" . }}
    </div>
{{ end }}

@rdwatters @bep I have my notes on naming variables in config.toml and accessing them from templates over here. Do they look right?

Hmmm…interesting. To my knowledge, I thought that "" != set. Please read the description for the default function:

I think it would be worthwhile to make the definition of “set” clear across all the templating functions, assuming that the definition mention above in the default function description is correct. Glad it worked out! Cheers.

Well, in the same snippet above, if I do the below instead:

{{ else if isset .Site "DisqusShortname" }}

or

{{ else if isset .Site "disqusshortname" }}

it always evaluates to false. So whether or not I have DisqusShortname = "foo", that condition never becomes true.

So looks like the problem is that I am doing something wrong when using isset for .Site variables (not that isset is returning true for empty string var). Any idea what’s wrong with above?

Or there’s just something quirky going on with the _internal Disqus template? Honestly, I never use isset. I think with and the other conditionals are much cleaner…

I would rule that out; I am not using the internal template…

<!-- snip -->
{{ else if .Site.DisqusShortname }}
    <div class="comments clear-float">
        <h1>Comments</h1>
        {{/* template "_internal/disqus.html" . */}}
        {{ partial "disqus" . }}
    </div>
<!-- snip -->

Ah, didn’t notice the /*...*/ in your original post. Have you tried $.Site then?

<!-- snip -->
{{ else if $.Site.DisqusShortname }}
    <div class="comments clear-float">
        <h1>Comments</h1>
        {{/* template "_internal/disqus.html" . */}}
        {{ partial "disqus.html" . }}
    </div>
<!-- snip -->

And I’m assuming that the partial is at layouts/partials/disqus.html

Also, if you think there is a bug with the way that isset is working, I would file an issue.

That too doesn’t work. Also, if {{ else if .Site.DisqusShortname }} worked, then {{ else if isset .Site "DisqusShortname" }} should also work, right?

And I’m assuming that the partial is at layouts/partials/disqus.html

Yes. The partial is working fine as I have tested the {{ else if .Site.DisqusShortname }} logic to work as expected.

I would file an issue.

OK, will do. I did not want file an issue for what could be my mistake in the template. Thanks for the discussion.

Well, it still might be, haha. Like I said, I never use isset.

Glad the discussion (maybe) helped :smile:

Done – isset doesn't always seem to work · Issue #3089 · gohugoio/hugo · GitHub