Dammit. I think I spoke too soon. I’ve run into a weird situation here.
If I add the following to my page:
<p>LAST MODIFIED:{{ .Lastmod.Format "02 Jan 2006" }} </p>
then run hugo server
the site builds without any problems. However, if I add a .GitInfo
variable:
<p> LAST MODIFIED: {{ .Lastmod.Format "02 Jan 2006" }} — BECAUSE: "{{.GitInfo.Subject }}"</p>
Then hugo server
bails out with this twice repeated error:
Building sites …
ERROR 2020/05/10 14:46:19 render of "page" failed: "/Users/madra/Sites/stiobhart.net/themes/stiobhart.net/layouts/_default/single.html:43:147": execute of template failed: template: _default/single.html:43:147: executing "_default/single.html" at <.GitInfo.Subject>: nil pointer evaluating *gitmap.GitInfo.Subject
ERROR 2020/05/10 14:46:19 render of "page" failed: "/Users/madra/Sites/stiobhart.net/themes/stiobhart.net/layouts/_default/single.html:43:147": execute of template failed: template: _default/single.html:43:147: executing "_default/single.html" at <.GitInfo.Subject>: nil pointer evaluating *gitmap.GitInfo.Subject
and the site fails to build. Yet, if I use the first option above, then run hugo server
so the site builds and only then add the {{.GitInfo.Subject }}
part into the template and re-save the template file, Hugo’s live reload reloads whichever page I’m viewing with that {{ .GitInfo.Subject }}
data correctly displayed on the page.
So how can Hugo read the content of the {{.GitInfo.Subject }}
variable after a live reload but [if I’m interpreting the error correctly] seems to be finding the variable unset when building from scratch?
I have set the enableGitInfo = true
option in my site’s config.toml
file.
EDIT:
I’m now thinking that this ‘double error’ above maybe actually be 2 single errors relating to two particular pages in my site which [for whatever reason] have no associated Git info and therefore Hugo is bailing out when it tries to render that meta info for those pages. Unfortunately [if that is the case] the error doesn’t pinpoint which pages are affected.
Obvious remedy is to add in a check for the presence of the .GitInfo.Subject
variable value, before trying to print it to the page. But, here again, I’m running into probs:
First I tried an isset
…
{{ if (isset .GitInfo "Subject") }}
{{.GitInfo.Subject }}
{{end}}
But Hugo doesn’t like that because the .gitmap
stuff is a pointer:
WARNING: calling IsSet with unsupported type "ptr" (*gitmap.GitInfo) will always return false.
So I tried again using with
instead, which is meant to be the recommended way to handle variables which may not exist
{{with .GitInfo.Subject }}
{{.GitInfo.Subject }}
{{end}}
But that gives me the following error:
ERROR 2020/05/10 15:10:50 render of "page" failed:
<snip>
can't evaluate field GitInfo in type string
So, how would I go about handling writing a conditional so I only try to display {{.GitInfo.Subject }}
if it actually exists for a particular page?