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?
{{ 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
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 }}
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…