Hi, I just rediscovered quick tip #004 and it inspired me a much simpler solution based on the principle that it’s possible to iterate through the collections
object itself.
The collections
object is an object keyed with tag names pointing to the collection of content containing that tag.
Loosely quoted from quick tip #004
So to list all tags with its own content, it would look something like this in Nunjucks:
template.njk
{% for tag, tagCollection in collections %}
{% if tag !== "all" %}
<figure>
<h2>{{ tag }}</h2>
<ul>
{% for post in tagCollection %}
<li>{{ post.data.title }}</li>
{% endfor %}
</ul>
</figure>
{% endif %}
{% endfor %}
Here, I use the if
statement to “filter out” the all
collection which is in collections
by default, but it could be use to remove any collection from the displayed result. The good news with this is that you don’t need a custom collection anymore which greatly reduces the overhead! If you want to see the content of collections
for yourself, you can use {{ collections | log }}
in any template to display the object in the console (works in version 0.11.0 and up)!