Problem with loop and scratch

Hi all!

I have about 250 Pages.

{{ range (where .Data.Pages "Section" "article").ByDate.Reverse }}                            
  page                                                                                                                                                                                                                        
{{ end }}

This prints 250 times ‘page’. OK

Now if I use any Scratch function it only get 22 times printed:

{{ range (where .Data.Pages "Section" "article").ByDate.Reverse }}                                                                                                              
  {{ $.Scratch.Add .Params.category 1 }}                                                                      
  page                                               
{{ end }} 

What’s going on here? Why?

Background of this: I added a category param to every page. Now on the startpage I want to show only 2 pages from every category sorted by date. ended up with this:

{{ range (where .Data.Pages "Section" "article").ByDate.Reverse }}                                                                                                                             
    {{ if lt ($.Scratch.Get .Params.category) 2 }}
      {{ $.Scratch.Add .Params.category 1 }}
      <div class="column">
        {{ .Render "list-item" }}
      </div>
    {{end}}
{{ end }} 

It should and do work, but the loop does not iterate over all pages, but breaks after ~ 22 pages … so some categories are not showed oder only one page instead of two

I would suspect the the loop exits because an error (which should show up in the log). I see several suspects, but I’m not spending time debugging your code. It looks very hacky.

What should be possible and much more elegant, something ala this:

$cat := $.Site.GetPage("taxonomyTerm", "categories")

Then you can iterate

range $cat.Data.Terms

And use the first func to fetch the first to pages of each term.