This is NOT a discussion! I'm reporting an already fixed issue I had looking for a comprehensible explanation
I've been TWO DAYS trying to figure out what could possibly be wrong with a super simple code that builds <img/> tags from paths in a Data File:
{{ $paginator := (.Paginate ( where ".Params.data" "!=" nil ) ) }}
<!-- Do something with $paginator (or not) -->
{{ range $offset, $pages }}
{{ $data := getJSON (printf "/data/%s.%s.json" (.Params.data) ($.Site.Language.Lang | default "en") ) }}
{{ end }}
Not that matters but, in any case, this is a sample of the Data File:
{
"name": "Anything to describe the Data File itself. Not really used in the Templates",
"picture": {
"path": "assets/images/somepicture.jpg"
}
}
Since I was told Paginator is static, accordingly to the syntax from the docs, by filtering out all results that do not have a specific FrontMatter Parameter — in this example, data — all Pages within the range is assumed to have it when that constructor is used (e.g. filter out in Header Partial but use it in Listing Template).
However, this fragment was constantly yielding me countless occurrences of this incomprehensible error:
Failed to create request for getJSON: parse /data/%!s(
).en.json: invalid URL escape "%!s"
This error occasionally was succeeded by:
Error while rendering "section" in "videos\": template: _default/list.html:164:26: executing "_default/list.html" at
).en.json: invalid URL escape "%!s"
Which prevented me from further debugging because I couldn't read in what printf was being translated into, even though I knew which files could be the culprits (videos directory). This cut-off was in both Windows' Prompt and Log File, with or without the verbose flag
I manually checked every single Content File that should be included in the listing using the only tip I had (the videos directory) and, later, by removing each folder/files and re-adding one-by-one, but the error persisted.
Because %s, regardless the exclamation mark between them is somewhat common to other programming languages with printf support, I suspected that .Params.data had no value or could not be set somehow, even though those without it should've been filtered out way earlier in the code while defining the Paginator.
I gave it a shot, added a "fallback" Data File and changed the getJSON to:
{{ $data := getJSON (printf "/data/%s.%s.json" (.Params.data|default "fallback") ($.Site.Language.Lang | default "en") ) }}
Being fallback the mutable part of the new Data File I've added. In case of errors, the path above would then be translated to, for example, /data/fallback.en.json, in the English version of the site.
And, as expected, the problem was fixed.
But I needed to be sure so I ran the server and, once again, checked manually every single listing page, in all available languages, looking for the <img/> tag created with the fallback image I've defined in this new Data File I've added specifically for this and there wasn't even one!
I insisted and generated all HTML files to perform a global file search for the path of this fallback image and, once again, no occurrences found.
So what the hell happened with this .Params.data? Pages without it should be filtered out and even if some slipped the where routine somehow, the defaulted value should be used but it never was!
This shouldn't empty/undefined then, no?
Please use https://discourse.gohugo.io/ for questions/troubleshooting. Also see Hugo Documentation.
Most helpful comment
Please use https://discourse.gohugo.io/ for questions/troubleshooting. Also see Hugo Documentation.