Range over pages ignoring the first one that matches a condition

I’m trying to loop over all the blog articles on our site but I want to ignore the FIRST item that has a front matter field “featured” set. We display a featured article at the top of the page but sometimes the content team leave this field in place in past featured articles so I still want to display them in our list.

I’m trying to do something like this:

{{ $paginator := .Paginate (where .Pages "Type" "blog") (after 1 .Data.Pages.Params.featured) }}
      {{ range $paginator.Pages }}
        {{ partial "blog/list" . }}
      {{ end }}

Something like this:

{{ $done := false }}
{{ range $paginator.Pages }}
  {{ if ne $done }}
    // we did not ignore anything yet
    {{ with .Data.Pages.Params.featured }}
      {{ $done = true }}
      // first time we encounter featured so set to true and don't print
    {{ else }}
      // no featured, so print
      {{ partial "blog/list" . }} 
    {{ end }}
  {{ else }}
    // $done is set, so print
    {{ partial "blog/list" . }}
  {{ end}}
{{ end }}

I am pretty sure there will be also be ways to collect the pages (your first line) so that the first item with featured is filtered out. The with .Data.Pages.Params.featured comes from your sample code. Not sure if that is the proper frontmatter check. I would assume it to be .Params.featured, but maybe something is missing in your code.

Hi pkollitsch, thanks for you response. With a couple of small changes I was able to get that working, much appreciated.

For future reference here’s the final code:

{{ $done := false }}
{{ $paginator := .Paginate (where .Pages "Type" "blog") }}  
{{ range $paginator.Pages }}
  {{ if eq $done false}}
    {{ with .Params.featured }}
      {{ $done = true }}
    {{ else }}
      {{ partial "blog/list" . }} 
    {{ end }}
  {{ else }}
    {{ partial "blog/list" . }}
  {{ end}}
{{ end }}

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