No items left with first/after combination

I use this code:

{{ range first 4 (after 8 (where .Data.Pages "Section" "portfolio")) }}

and get this error:

error calling after: no items left in theme/partials/portfolio-4-col.html

This is correct as there are only 2 items left to display. The page is displayed anyway but is there a way to make this error go away?

Hi, i am on the road.

{{ range after 1 (first 5 (where .Data.Pages "Section" "portfolio" )) }}

Hope this helps

No that does not work.

Maybe I was unclear.

I do the following in one and the same template:

{{ range first 4 (where .Data.Pages "Section" "portfolio") }}
do stuff

{{ range first 4 (after 4 (where .Data.Pages "Section" "portfolio")) }}
do the same stuff with the next 4 items

{{ range first 4 (after 8 (where .Data.Pages "Section" "portfolio")) }}
do the same stuff with the next 4 items

So what I need is to slice the range to 4 items each.

Currently there are only 6 items at all, so I get the error message on the second and third slice.

So I’m not sure of the desired output, but looks like someone was already able to chunk content into group in a modulus-kinda way here:

Other than that, maybe the way I’m currently doing it on a homepage will provide some insight. I needed to break up my rows into 1-3-2…

{{- range first 1 ((where .Site.Pages "Section" "posts").ByPublishDate.Reverse) -}}
{{- range after 1 (first 4 (where .Site.Pages "Section" "posts").ByPublishDate.Reverse) -}}
{{- range after 4 (first 6 (where .Site.Pages "Section" "posts").ByPublishDate.Reverse) -}}

I am getting this error too and it prevents site generation.

It’s logical for after to throw an error when it doesn’t have at least N+1 items. You should protect your logic with an if-statement:

{{ if gt (len (where .Data.Pages "Section" "portfolio")) 8 }}
{{   range first 4 (after 8 (where .Data.Pages "Section" "portfolio")) }}

You should also pull repeated where clauses into a variable.

1 Like

Yes, I was having the same problem and this was the solution. Thanks a lot!