I see the ability to create new local variables and Scratch variables, but what I was hoping to do was check to see if a page level Param variable existed, and if not go ahead and create it one of my theme templates before calling an internal template which uses that variable.
More specifically, I was hoping to check to see if a post’s disqus_url was set, and build a new default if not.
So that’s the initial question, and if answered, I’ll know more than I do now and that’ll come in handy even though I expect my actual scenario for wanting to do this in this particular case is, well, dumb.
I fully expect one answer to be something to the effect of, “Um, why would you do this?”
The answer to that mostly is because I’m new to Hugo and don’t know any better, and that there’s a huge mess of not understood junk cluttering what the real solution to the original problem is. On the off chance that someone actually wants to tackle any of that, here’s the situation.
I am messing around with Hugo and making a new site, which I’m looking at locally with Hugo server, and wish to be able to view internal to my lan by various readers across various devices. After that, I’m pushing it to gitlab and using their typical pages CI workflow to deploy it there. I’m also looking at netlify and have it set up to deploy there on a push to gitlab. Yeah, probably excessive but one of the main reasons for any of this was trying out different generators, workflows, hosts, services, etc.
So, based on the above, I have some deploys with the baseURL being relative, some with it being a subdir on a host (gitlab/github project pages for example), some with it being at the root of the host (gitlab/github user/org pages, netlify, etc.).
I figured I’d just keep baseURL in the config.toml file set to “/”, then override it as needed on the command line in the build/deploy step. I found I needed to set the baseURL to the full base, with host, in any actual deployment where I wanted disqus to work, because otherwise it would try to embed with the relative url for the disqus_url (which isn’t cool, according to disqus, https://help.disqus.com/customer/en/portal/articles/472098-javascript-configuration-variables )
As an aside, I don’t really understand the differences between the universal code they provide and the code generated in template_embedded.go, in terms of the config function vs. the differently named/set vars.
So, since relative URLS weren’t cool for the disqus url, and since I noticed in the implementation code (bad user!) that if the param disqus_url was set, that would be used rather than the permalink, I thought I’d kill 2 birds with 1 stone, and thought I’d make a new site level config called DisqusBaseURL, and then combine that with the relative version of the permalink to get a single name to use. I’m not sure that’s really something I want to do or that will even work as expected, since I thought the disqus_identifier was actually what was used to look up and, ahem, identify, the threads. But I still wanted to give it a shot.
First I naively (given, this is all kind of that…) tried to put something like
disqus_url={{with .Site.DisqussBaseURL}}{{ . }}/{{ .Permalink }}{{ end }}
In my default.md archetype file’s front matter. I guess I was at first thinking that when I did a
hugo new post/whatever.md
That it would at that stage process the template stuff and leave me with a disqus_url all set. That’s kind of dumb upon reflection, but I’ll admit to it anyway. Next I put quotes around the template stuff thinking that it would be stuck in the generated default front matter as a string (and it was), then might be processed when Hugo actually did the site generation (It wasn’t). I tried a shortcode version too, but I guess nothing is ever processed from frontmatter like that.
So, then I figured I’d just go ahead and set the disqus_url in the template, right before the call to the internal disqus template. I figured I’d check to see if it had already been set, and only if it was not, and only if .Site.DisqussBaseURL was set, then I’d combine them and assign it to disqus_url. … and here we are - I discovered I didn’t see any way to actually set the variable at that point.