8

我最近发现了ZolaTera(用于静态生成网站的 Rust 框架)并发现它们很棒。

我正在尝试过滤特定类别页面以显示在同一页面的某个部分中。为了说明,我写了一些这样的代码:

<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>

对于不同的类别,上面的代码应该有多个部分,例如“rust”、“java”等。

我编写了代码来解释我的问题,但这不是我想要的方式(并且当部分重复时它不起作用)。

加载部分/页面时如何过滤特定类别?

内容文件中的前端元数据是:

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

如果您在上面看到我的示例代码,我必须首先通过哈希映射访问它,然后是数组,以便过滤所有“生锈”的页面。

下面的过滤器不起作用:

for page in section.pages | filter(attribute="taxonomies.categories", value="rust"
4

1 回答 1

2

我设法解决了它。首先,我做了这样的测试:

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>

我能够选择 .md 文件中设置的标题。

所以我继续前进,我做到了:

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

上面的代码将过滤类别分类的页面。

于 2018-09-26T16:38:59.950 回答