How to filter Taxonomies using Rust-based Zola / Tera?
Asked Answered
A

1

9

I have recently discovered Zola and Tera (Rust frameworks for statically-generated websites) and found them amazing.

I'm trying to filter specific category pages to display in a section on the same page. To illustrate, I wrote some code like this:

<div class="content">
    {% block content %}
    <div class="list-posts">
        {% for page in section.pages %}
        {% for key, taxonomy in page.taxonomies %}
        {% if key == "categories" %}
        {% set categories = taxonomy %}
        {% for category in categories %}
        {% if category == "rust" %}
        <article>
            <h3 class="post__title"><a href="{{ page.permalink }}">{{ page.title }}</a></h3>
        </article>
        {% endif %}
        {% endfor %}
        {% endif %}
        {% endfor %}
        {% endfor %}
    </div>
    {% endblock content %}
</div>

There should be MULTIPLE sections of the code above for different categories, e.g. "rust", "java", etc.

I wrote the code to explain my question, but it isn't the way I want it (and it doesn't work when the sections are duplicated).

How do I do the filtering of the particular category when the sections/pages are loaded?

The front-matter metadata in the content file is:

title = "A web page title"
[taxonomies]
categories = ["rust"]

If you see my example code above, I have to access it first via a hash map, then an array, in order to filter all pages which is "rust".

The filter below doesn't work:

for page in section.pages | filter(attribute="taxonomies.categories", value="rust"
Acquainted answered 25/9, 2018 at 2:44 Comment(7)
Your question has only a little to do with rust (the two frameworks are rust ones, but your code does not contain any rust at all), so I wouldn't tag it as rust, but because there is no Tera tag, I think it's okay?!Linctus
I have never used tera, but doing some research gave me tera.netlify.com/docs/templates/#filter , which should answer your question (if I understood you correctly).Linctus
I removed rust tagAcquainted
I tried the "filter" which you told me, but it is not simple because I need to access it inside a hash map, and then an array. This: { for page in section.pages | filter(attribute="taxonomies.categories", value="rust" } didn't work.Acquainted
IMO, it was not stupid to put a Rust tag. This is a Rust template engine, and without this tag, you have no visibility at all.Heterocercal
Yeah, I put back the rust tag.Acquainted
Your question text doesn't make it quite clear to me what you actually want the end result to be.Bagby
A
4

I managed to resolve it. First, I did tests like this:

HTML test print output
{% set categories = get_taxonomy(kind="categories") %}
{% set rustItems = categories.items | filter(attribute="name", value="rust") %}
{% set javaItems = categories.items | filter(attribute="name", value="java") %}
{{ rustItems[0].pages | length }}
<br>
{{ rustItems[0].pages[0].title }}
<br>
{{ rustItems[0].pages[1].title }}
<br>

I was able to pick up the title as set in the .md file.

So I moved on further and I did:

{% set categories = get_taxonomy(kind="categories") %}
{% set category = categories.items | filter(attribute="name", value="business") | first %}
{% for page in category.pages %}
{{ page.title }}
... etc.

The above code will filter the pages for category taxonomy.

Acquainted answered 26/9, 2018 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.