aboutsummaryrefslogtreecommitdiff
path: root/build/loop/index.html
blob: 4488987727db6195956966f2eb6079c695aa0c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!doctype html>
<html lang="en" id="top">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="icon" href="data:,">
	<title>Looping Through Jekyll Collections</title>
	<link href="https://bt.ht/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
	<style>*{box-sizing:border-box;}body{font-family:sans-serif;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{overflow:auto;}table{text-align:left;width:100%;}</style>
</head>

<nav>
  <a href="#menu">Menu &darr;</a>
</nav>

<main>
<h1>Looping Through Jekyll Collections</h1>
<p>2022-08-12</p>
<p>I recently needed to add a couple new items to my wife's personal recipe website (<a href="https://cookingwith.casa">cookingwith.casa</a>) which I hadn't touched in quite a while. The Jekyll build still worked fine, but I realized I was statically adding each <code>collection</code> by hand on the main homepage[^1].</p>
<p>Not so good.</p>
<p>Of course, this wasn't difficult at all to fix. Now everything is much more "hands free" moving forward. I figured I would share the details here in the hopes that others mind find it useful. Plus, it's my blog - so I'll do what I want!</p>
<h2>Looping Our Collections</h2>
<p>We want Jekyll to make things as streamlined as possible for us. This means that if I decide to add a new collection it will automatically render it along the others on the homepage.</p>
<p>Work smart not hard!</p>
<p>Let's take a look at the bare-bones collections loop:</p>
<pre><code>{% for collection in site.collections %}
    &lt;!-- Our code goes here --&gt;
{% endfor %}
</code></pre>
<p>Then we need to include an <code>if</code> statement to avoid pulling in standard <code>post</code> items (or leave this in if that is desired):</p>
<pre><code>{% for collection in site.collections %}
    {% if collection.label != 'posts' %}
    {% endif %}
{% endfor %}
</code></pre>
<p>Now for my specific use case, we want to display each collection label and then list its corresponding items below that label (see the <code>site[collection.label]</code> for reference)</p>
<pre><code>{% for collection in site.collections %}
    {% if collection.label != 'posts' %}
        &lt;h2&gt;{{ collection.label }}&lt;/h2&gt;
        &lt;ul class=&quot;recipe-list&quot;&gt;
        {% for item in site[collection.label] %}
            &lt;li&gt;
                &lt;a href=&quot;{{ item.url }}&quot;&gt;{{ item.title }}&lt;/a&gt;
            &lt;/li&gt;
        {% endfor %}
        &lt;/ul&gt;
        &lt;hr&gt;
    {% endif %}
{% endfor %}
</code></pre>
<p>That's it! Now if I plan to add any new collections down the line, I just need to include it in the <code>_config.yml</code> file and I'm set. The homepage will take care of the rest once rendered.</p>
<p>Enjoy looping through your Jekyll collections!</p>
<h2>Refs</h2>
<ol>
<li>Just the sections were statically rendered. All the recipes were pulled in dynamically - I'm not that insane!</li>
</ol>
<footer role="contentinfo">
    <h2>Menu Navigation</h2>
    <ul id="menu">
        <li><a href="/">Home</a></li>
        <li><a href="/projects">Projects</a></li>
        <li><a href="/uses">Uses</a></li>
        <li><a href="/wiki">Wiki</a></li>
        <li><a href="/resume">Resume</a></li>
        <li><a href="/colophon">Colophon</a></li>
        <li><a href="/now">Now</a></li>
        <li><a href="/donate">Donate</a></li>
        <li><a href="/atom.xml">RSS</a></li>
        <li><a href="#top">&uarr; Top of the page</a></li>
    </ul>
    <small>
        Built with <a href="https://git.sr.ht/~bt/barf">barf</a>. <br>
        Maintained with ♥ for the web. <br>
        Proud supporter of <a href="https://usefathom.com/ref/DKHJVX">Fathom</a> &amp; <a href="https://nextdns.io/?from=74d3p3h8">NextDNS</a>. <br>
        The content for this site is <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.<br> The <a href="https://git.sr.ht/~bt/bt.ht">code for this site</a> is <a href="https://git.sr.ht/~bt/bt.ht/tree/master/item/LICENSE">MIT</a>.
    </small>
</footer>