Range Question

Say I have a list of 90 names. Each name points to a different page.

How can I display this list in 3 columns?

I am aware of {{ range first 30 .Data.Pages }} and {{ range last 30 .Data.Pages }}

But how can I populate the middle column with the names between 31-60 using range?

Or is there another way?

I’ ve been looking at the Docs and searching this forum but I just can’t find a solution -so far-.

Have a look at the after template function. In combination with first you can retrieve the desired pages for the second column.

@digitalcraftsman
Thanks for the tip. I know about the after function but I haven’t found a way to combine it with first for now.

As a temporary workaround I have come up with this solution:

                     <ul class="column">
    {{ range .Data.Pages }}
        {{ if eq .Params.col "middle" }}
          <li>
              <span><a href="{{ .Permalink }}">{{ .Title }}</a></span>
            </li>
        {{ end }}
      {{ end }}
              </ul>

Specified a parameter for the names that I need to appear in the middle column.

It’s less than ideal since the list of names will be changing, but I guess it will have to do for now.

Well, first of all you’ve to create an array of all elements after index 30. From this new list you’ve to take the first 30 elements. In code it would look as follows:

{{ $middleCol := first 30 (after 30 .Data.Pages) }}
1 Like

Thanks a lot man!

Here is the final code snippet that works for me:

<ul class="column">
{{ $middleCol := first 30 (after 30 .Data.Pages) }}
{{ range $middleCol }}
   <li>
          <span><a href="{{ .Permalink }}">{{ .Title }}</a></span>
        </li>
        {{ end }}
</ul>
1 Like

Glad this worked out. I should mention there is an example of this here too:

https://hugodocs.info/functions/after/#example-of-after-with-first-2nd-ndash-4th-most-recent-articles

If you can think of any improvements @alexandros, please let me know!

Thanks for the link. I’ll check it out.

1 Like