8

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"
Newbyte
  • 2,421
  • 5
  • 22
  • 45
ikevin8me
  • 4,253
  • 5
  • 44
  • 84
  • 1
    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?! – hellow Sep 25 '18 at 06:03
  • 1
    I have never used tera, but doing some research gave me https://tera.netlify.com/docs/templates/#filter , which should answer your question (if I understood you correctly). – hellow Sep 25 '18 at 06:06
  • I removed rust tag – ikevin8me Sep 25 '18 at 08:45
  • 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. – ikevin8me Sep 25 '18 at 08:48
  • 1
    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. – Boiethios Sep 25 '18 at 13:08
  • Yeah, I put back the rust tag. – ikevin8me Sep 25 '18 at 13:16
  • Your question text doesn't make it quite clear to me what you actually want the end result to be. – Cubic Sep 26 '18 at 14:13

1 Answers1

2

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ikevin8me
  • 4,253
  • 5
  • 44
  • 84