Hi, I’m getting a strange results while using range first 1 .Params.Images
with a site using a bit more nested content structure, than just root content sections. I have a “products” section with “product1” and “product2” sections inside it. I defined type
for these sections and created list and single layouts of these types. And everything is fine except an error while using range first 1 .Params.Images
in subsection’s layout - it throws an error, while range .Params.Images
is working normally, providing a list of images URLs. And the same code throws no errors while using it from section layout, range first 1 .Params.Images
outputs the first image URL from markdown. Couldn’t find any info about that by googling and reading docs. What am I missing?
It picks up the image list from the page’s frontmatter?
You need to set that for the subsections or you can perhaps use “cascade” if the images of the subsections are the same ones.
As an aside, using the image URL and static images is perhaps not the best way, you might be better referring to images as assets so you can process them (e.g. responsive, retina, webp versions)
Yes, it picks up images list from md for every product, all images are different for every product. Images aren’t static, they are in assets directory and processed. If I process the whole list of images - everything is fine, if I try to get just the first image from the list - there is an error with range first 1
. If I use a layout from root category to process subcategory - everything is still fine, range first 1
finds the first image for product in subcategory. But I need 3 different layouts to work, as products and their options are different.
Is this the error message?
executing “main” at <first 1 .Params.images>: error calling first: both limit and seq must be provided
Yes, exactly.
I suspect that one or more of the pages rendered by this template does not have an images key in front matter. You can code defensively:
{{ with .Params.images }}
{{ range first 1 . }}
{{ with resources.Get . }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ errorf "Unable to find image %s" . }}
{{ end }}
{{ end }}
{{ end }}
All pages have images key. And I don’t want to use with, as I need context for images alts and titles.
Well, the error is cause by the first
function because it’s receiving a nil parameter. And based on what you described, the only way I can reproduce the problem is with a page missing the key.
You can use with
to grab the context like:
{{ with $foo := .Params.images }}
Or use if
for the outer conditional.
I left only one file in the directory to be sure, with images key. And a straight output to page of keys list works normally, till I try to get first, last or index key.
Run this:
{{ range where site.RegularPages "Params.images" nil }}
{{ warnf "%s" .}}
{{ end }}
Otherwise, I have no idea without seeing your project.
Or this to look at section pages too, in case you have similar code in a list/home template.
{{ range where site.Pages "Params.images" nil }}
{{ warnf "%s" .}}
{{ end }}
Omg, there is another section with a name similar to “product1”, containing articles, so hugo started to process that articles section with list layout named “product1”, and I wasn’t aware of this, cause I don’t use list layout to display content in articles section, only single page layout, which was terminating to default single layout. No chances to debug this by reading html, but with {{ warnf "%s" .}}
the problem became visible. Thank you.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.