Looping through Data Files in Groups

Hi all, I’m having trouble with looping through data files. I want to “group” data files within a row div, but I can’t seem to figure out how to limit the range iteration to do so.

Below is my index.html file. This currently just loops through all data files and places them in the same row:

<!DOCTYPE html>
<html lang="en">

    {{ partial "header.html" . }}

    <body>
        <div class="container">
            {{ partial "profile.html" . }}

            <div class="row">
            {{ range $.Site.Data.projects }}
                {{ partial "project.html" . }}    
            {{ end }}
            </div>
        </div>
    </body>

</html>

In psuedo code, I want something similar to this:

{{ range SELECT NEXT THREE FROM $.Site.Data.projects }}
    <div class="row">
    {{ range THE THREE FROM ABOVE }}
        {{ partial "project.html" . }}    
    {{ end }}
    </div>
{{ end }}

For a little more context, can you tell me more about the data files in $Site.Data.Projects?

Here’s an example of one of the TOML files:

name = "Project Title"
thumbnail = "project/cover.png"
playlink = "https://www.google.com"
description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae."

Figured it out! For anyone else that has a similar problem:

{{ range $index, $element := sort $.Site.Data.projects "date" "desc"}}
                
    {{ if eq (mod (sub $index 3) 3) 0 }}
            <div class="row">
    {{ end }}
    
    {{ partial "project.html" $element }}
    
    {{ if eq (mod (sub $index 2) 3) 0 }}
            </div>
    {{ end }}
            
{{ end }}
1 Like