Just started playing with Zola and wondered if there was a way to group sections into a single collection that I could loop over?
The idea behind it is to have different sections like “article”, “photoset” or “bookmark” all with dates, but be able to view all of them in one feed reverse chronologically.
I’ve worked out how to get each using the section.subsections from the homepage and then get_section() with the results, but is there a way to merge and then sort these arrays?
So in this case I’d wrap all my sections in a pseudo-section with all it’s children having transparent: true? That would let me iterate over them all at once.
Pushing my luck here, anyway to then also be able to view them individually? Would the get_section() approach work in this instance?
I opted not to go with transparent only because it brings in other pages such as /about which don’t naturally have dates and then need filtering out of the loop.
Instead this a demonstrative example of what I did
{% set blog = get_section(path="blog/_index.md") %}
{% set photos = get_section(path="photos/_index.md") %}
{% set feed = blog.pages | concat(with=photos.pages) %}
{% for page in feed | sort(attribute="date") | reverse %}
{% set section = page.components | first %}
{% if section == "blog" %}
<li><a href="{{ page.permalink | safe }}">{{ page.title }} - {{ page.date }}</a></li>
{% endif %}
{% if section == "photos" %}
<img src="{{ page.extra.lead_image }}" />
<li><a href="{{ page.permalink | safe }}">{{ page.title }} - {{ page.date }}</a></li>
{% endif %}
{% endfor %}
I preferred this solution as I’m likely to only ever have half a dozen sections I want to iterate over and it works better than transparent with other filters such as slice. An example was that I was fudging a date onto my other pages and simply not rendering them with my section check. However that threw off my slice filter if any of the dates happened to fall in the range I was after.
I have to tip my hat to the creators of Zola. It’s a very simple generator to understand (my other experiences are Hugo and Publish) but you can combine simple tools to order the data in quite complex ways. I also really appreciate the template language and explicit definition of template for each item of content (looking at you Hugo). Great work!