{{ range first 1 (where .Data.Pages "Type" "post") }}
<div class="ph1-ns w-50-ns flex">
{{ .Render "li" }}
</div>
{{ end }}
which successfully pulls data for one blog post.
I have another partial comic.html with the exact same code:
{{ range first 1 (where .Data.Pages "Type" "post") }}
<div class="ph1-ns w-50-ns flex">
{{ .Render "li" }}
</div>
{{ end }}
but this fails with the message:
2:18:57 PM: ERROR 2018/08/11 04:18:57 Error while rendering “section”: template: /opt/build/repo/site/layouts/section/products.html:3:3: executing “main” at <partial “jumbotron” …>: error calling partial: template: partials/jumbotron.html:6:11: executing “partials/jumbotron.html” at <partial “comic” .>: error calling partial: template: partials/comic.html:6:19: executing “partials/comic.html” at <where .content.Data…>: error calling where: can’t iterate over <nil>
Notes:
blog partial is directly called from index.html using: {{ partial "blog" . }}
comic partial is indirectly called from index html via jumbotron.html index.html: {{ partial "jumbotron" (dict "imageUrl" .Params.image "title" .Title "subtitle") }} jumbotron.html: {{ partial "comic" . }}
I suspected this might have been caused by not passing the context to jumbotron, so tried:
{{ partial "jumbotron" (dict "imageUrl" .Params.image "title" .Title "subtitle" .Params.subtitle "content" .) }}
and then: {{ range first 1 (where .content.Data.Pages "Type" "post") }}
but this also didn’t work. I even tried declaring a variable using .Site.GetPage and then referencing this variable for the .Data.Pages data, but it didn’t work. Any help would be appreciated.
Hard to say without a repo but as I understand your jumbotron partial whose context is a dict calls the comic partial whose context is the dot.
The dot you are passing to comic is the context of the jumbotron partial which is a dict. You should add the page context to your dict and pass it to the comic partial.
Okay… I misread your initial post, sorry about that.You seem to have fixed the context issue you had, anyhow if it was a context issue the error would mention an object which does not exist.
The problem seems to be that at some point one page using your layout does not have a .Data.Pages are you sure this partial is only used with list templates ? Never on single ones ?
You can try wrapping your ranging with:
{{ with .content.Data.Pages }}
etc...
{{ end }}
And see how it goes, and if you’ve got a repo, this would make the helping easier
The problem seems to be that at some point one page using your layout does not have a .Data.Pages are you sure this partial is only used with list templates ? Never on single ones ?
^^ what makes you think that? The error message makes no reference to another template. Only comic.html which contains .Data.Pages or .content.Data.Pages (depending on which solution I am trying), but both solutions lead to the same error message.
Question 2
Why would wrapping the range with:
{{ with .content.Data.Pages }}
etc...
{{ end }}
have a different result to: {{ range first 1 (where .content.Data.Pages "Type" "post") }} without the wrapping?
I tried this:
{{ with .content.Data.Pages }}
{{ range first 1 (.Data.Pages where "Type" "post") }}
<div class="ph1-ns w-50-ns flex">
<!-- // .Render "li" // -->
</div>
{{ end }}
But could not get it to work. The following error kept being generated:
10:03:53 AM: ERROR 2018/08/12 00:03:53 Error while rendering “home”: template: /opt/build/repo/site/layouts/index.html:3:3: executing “main” at <partial “jumbotron” …>: error calling partial: template: partials/jumbotron.html:6:11: executing “partials/jumbotron.html” at <partial “comic” .con…>: error calling partial: template: partials/comic.html:6:33: executing “partials/comic.html” at <.content.Data.Pages>: can’t evaluate field content in type *hugolib.PageOutput
Question 3
I also tried passing {{ partial "comic" .content }} in jumbotron.html, but received the following error:
executing “partials/comic.html” at <.content.Data.Pages>: can’t evaluate field content in type *hugolib.PageOutput
What does the above error mean? I couldn’t find any info online.
FYI Also, my repository is not public because the website and content is directly generated from the repository, and the content needs to remain private.
@regis, I read your last comments carefully and reread them, and the content of the link you sent, and still have not been able to make sense of this problem. If you could help me understand what I have done wrong, that would be much appreciated. I have spent over 6hrs on this problem.
In all probability you can simply change the above to just .Pages.
I also think that you are missing the outer context of your partial. To fix that, you need to insert the dollar sign $ in the pages variable for example $.Pages
However if the above doesn’t work then try replacing the .Data.Pages with .Site.RegularPages (with and without the $ dollar sign).
More info about the Site.Pages variable here:
If all of the above fails to fix your issue then we must see your source code to help further.
Since the content of your Hugo project needs to remain private then replace it with dummy content.
I managed to solve the issue, and it was due to what you suspected. The issue was not my lack of understanding of context, but the fact that I didn’t realize there were other layouts using the “jumbotron” partial. The clue was in the error message:
Error while rendering “section”: template: /opt/build/repo/site/layouts/section/products.html
I was editing index.html and that page was not the one causing issues. It took a long time to diagnose because I am working with a boilerplate template, and am not familiar with error logs in this language. Hopefully the next problem will become easier to diagnose