Looping through collection files

Hi all,

Collection basics as I understand them:

  • add info to the _config.yml and output:true to make them get processed as pages.
  • create corresponding folder in top level directory with the same name as in the config.yml and with a leading underscore.
  • Add md or html files to that folder with some front matter and voila.

I successfully created a collection entitled libguides and successfully created a for loop on my homepage that created links to each page.

Now I’m trying to do the same thing for a collection entitled libcal and am not able to create a for loop.

The thing i’m stumbling on is this: {% for libcal in site.libcal %}
I don’t know what to put where it says libcal immediately after for

My loop that actually working is thus: {% for libguide in site.libguides %} The collection name is libguides. Nowhere anywhere in any of my code have I used the singular version (libguide) except in that for loop and somehow it works.

I got the idea to try ‘libguide’ when I saw documentation that would just put the singular name of the collection in the for loop. I have no idea why this is working.

Two other pages I read for help today had these examples and I have no idea why they’re using what they are for the word immediately after for.
{% for topic in site.interviews %}
{% for album in site.music limit:3 %}

Thanks for any help.

You can name the bit after for whatever you want. It’s used to access that object inside of the loop. It’s really up to you and how you want to name things. The plural/singular naming scheme is good.

{% for foo in site.libcal %} will work just as well as {% for cal in site.libcal %}. Just as long as it’s unique and makes sense to you it doesn’t really matter.

1 Like

If you want to be consistent with libguides then maybe rename the collection to libcals.

Then you can do {% for libcal in site.libcals %}

1 Like

Fascinating. Thank you for the help!

I like to use item as the thing after For - it makes it so much more clear that it isn’t really a thing. I think a lot of people see:

for post in site.posts

and think that post on its own is something - like post.title but post only exists in the context of the for loop.