When learning new things, it always go slowly at the beginning, speeding things up as the learning curve progresses. In the end, every other tool in the developer's toolset helps in the long run. Today I dug deeper into another template engine called Tera.

Tera is a template engine for Rust based on jinja which was made for Python instead. I was writing about jinja a few months ago, mostly under tag ansible, as ansible uses jinja as it's template engine. Jinja and Tera are pretty similar, although Tera is not marketed to be fully feature-complete with with predecessor, jinja. Anyway, for my goal to get into Rust eventually, learning Tera seems like a worthwhile thing to do. For now, I still did not find any stopper as a go-to SSG tool for my blog, and it looks like some people are starting to notice my work, which are all more compelling reasons to continue.

What is an archive

An Archive is a list of all posts, usually grouped by some time period, for instance a year or a month. Such list is pretty important in my opinion as it allows me to gauge the work I did easily, not to mention simpler visual searching. Unfortunately, I did not know how to search for a guide implementing such feature, as even though it might be pretty obvious.

What I do usually is to browse repository issues for keywords. Since I did not know I am looking for an archive keyword, the best thing I could found was in #435. I did not find a solution there. After a few days of silence I actually stumbled upon the archive Zola docs that got me moving again. The most important part of the code is this:

{% for year, posts in section.pages | group_by(attribute="year") %}
...
{% endfor %}

It wasn't a ready to use code snippet as there were some bits missing, but at least I knew what to look for.

Getting the right pages

Since my main _index.md was already serving paginated results I had to find another way to get all the posts to create a list of them. I have found a next clue in the #628 in the form of get_section(), specifically:

{% set s = get_section(path="posts/_index.md") %}

Finally I got a way to get all the pages to do something with it.

Sorting the results

The pages I got this way however were out of order. Setting a sort_by = "date" in the archive/_index.md had no effect, which I did not expect. I was able to sort them using Tera filters, specifically via sort(attribute="date"). The full working templates/archive.html template looks like this:

{% extends "index.html" %}

{% block content %}
  {% set section = get_section(path="blog/_index.md") %}
  {% for year, posts in section.pages
    | sort(attribute="date")
    | reverse
    | group_by(attribute="year") %}
  <div class="archive">
    <h2>{{ year }}</h2>
    <ul>
      {% for post in posts %}
      <li>
        <time>{{ post.date | date(format="%d-%h") }}</time>
        <a href="{{ post.permalink }}">{{ post.title }}</a>
      </li>
      {% endfor %}
    </ul>
  </div>
  {% endfor %}
{% endblock content %}

To actually see the results, the aforementioned archive/_index.md works by referencing this temple:

+++
template = "archive.html"
+++

No other code is strictly required, although some styling definitely helps. It is possible to check it under Archive.