This is a question mainly to do with Tera, but could be relevant to Zola as well if the required functionality could be added there.
I’ve got a macro I use in multiple websites to build a little ‘metadata’ section on page listings.
{% macro meta(item) %}
<dl class="meta">
{% if item.date %}
<dt>{{ trans(key="date") }}</dt>
<dd>{{ item.date | date(format="%Y-%m-%d") }}</dd>
{% endif %}
{% if item.taxonomies %}
{% for key, value in item.taxonomies %}
<dt>{{ key | title }}</dt>
<dd>{{ value | join(sep=", ") }}</dd>
{% endfor %}
{% endif %}
</dl>
{% endmacro %}
This works well for quickly listing the taxonomies a page belongs to, but has one caveat: Taxonomies are not sorted. For example, if all pages in a section belonged to an author, and were part of several categories, one page might show the author first, while another might show categories first. E.g.
<ul>
<li>
<h2>Post 1</h2>
<dl class="meta">
<dt>Author</dt>
<dd>Author 1</dd>
<dt>Categories</dt>
<dd>Category 1, Category 2</dd>
</dl>
</li>
<li>
<h2>Post 2</h2>
<dl class="meta">
<dt>Categories</dt>
<dd>Category 2</dd>
<dt>Author</dt>
<dd>Author 2</dd>
</dl>
</li>
</ul>
I have tried sorting the taxonomy using {% for key, value in item.taxonomies | sort(attribute=”key”) %}, but this does not work in Tera. My question is: is there a way of sorting a HashMap / associative array in Tera, or would it be possible to change the way fields on a page are serialized to the template so that the order of properties is consistent (using something like serde-ordered-collections)?