# A bit strange behaviour of range first 1

**URL:** <https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266>\
**Category:** support\
**Created:** [March 3, 2023, 9:18pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266 "2023-03-03T21:18:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vassap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vassap/32/18701_2.png) [@vassap](https://discourse.gohugo.io/u/vassap)\
**Post date:** [March 3, 2023, 9:18pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/1 "2023-03-03T21:18:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![andrewd72](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/andrewd72/32/18371_2.png) [@andrewd72](https://discourse.gohugo.io/u/andrewd72)\
**Post date:** [March 3, 2023, 9:32pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/2 "2023-03-03T21:32:49Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![vassap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vassap/32/18701_2.png) [@vassap](https://discourse.gohugo.io/u/vassap)\
**Post date:** [March 3, 2023, 9:44pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/3 "2023-03-03T21:44:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:04pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/4 "2023-03-03T22:04:09Z")

</div>

Is this the error message?

> executing “main” at \<first 1 .Params.images\>: error calling first: both limit and seq must be provided

---

<div class="post-metadata">

**Author:** ![vassap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vassap/32/18701_2.png) [@vassap](https://discourse.gohugo.io/u/vassap)\
**Post date:** [March 3, 2023, 10:15pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/5 "2023-03-03T22:15:19Z")

</div>

Yes, exactly.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:20pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/6 "2023-03-03T22:20:36Z")

</div>

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:

```auto
{{ 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 }}

```

---

<div class="post-metadata">

**Author:** ![vassap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vassap/32/18701_2.png) [@vassap](https://discourse.gohugo.io/u/vassap)\
**Post date:** [March 3, 2023, 10:22pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/7 "2023-03-03T22:22:43Z")

</div>

All pages have images key. And I don’t want to use with, as I need context for images alts and titles.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:25pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/8 "2023-03-03T22:25:30Z")

</div>

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:

```auto
{{ with $foo := .Params.images }}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:26pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/9 "2023-03-03T22:26:32Z")

</div>

Or use `if` for the outer conditional.

---

<div class="post-metadata">

**Author:** ![vassap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vassap/32/18701_2.png) [@vassap](https://discourse.gohugo.io/u/vassap)\
**Post date:** [March 3, 2023, 10:31pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/10 "2023-03-03T22:31:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:32pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/11 "2023-03-03T22:32:54Z")

</div>

Run this:

```auto
{{ range where site.RegularPages "Params.images" nil }}
  {{ warnf "%s" .}}
{{ end }}

```

Otherwise, I have no idea without seeing your project.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:39pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/12 "2023-03-03T22:39:01Z")

</div>

Or this to look at section pages too, in case you have similar code in a list/home template.

```auto
{{ range where site.Pages "Params.images" nil }}
  {{ warnf "%s" .}}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:39pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/13 "2023-03-03T22:39:05Z")

</div>

Made this topic a personal message on March 3, 2023, 10:39pm UTC



---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 3, 2023, 10:41pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/14 "2023-03-03T22:41:11Z")

</div>

Made this topic public on March 3, 2023, 10:41pm UTC



---

<div class="post-metadata">

**Author:** ![vassap](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vassap/32/18701_2.png) [@vassap](https://discourse.gohugo.io/u/vassap)\
**Post date:** [March 4, 2023, 10:12pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/15 "2023-03-04T22:12:22Z")

</div>

> [@jmooring](#):
>
> ```auto
> {{ range where site.RegularPages "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.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [March 6, 2023, 10:12pm UTC](https://discourse.gohugo.io/t/a-bit-strange-behaviour-of-range-first-1/43266/16 "2023-03-06T22:12:28Z")

</div>

Closed on March 6, 2023, 10:12pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
