我最近发现了Zola和Tera (静态生成网站的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"]如果你看到我上面的示例代码,我必须首先通过散列映射访问它,然后是一个数组,以便过滤所有"rust“的页面。
下面的筛选器不起作用:
for page in section.pages | filter(attribute="taxonomies.categories", value="rust"发布于 2018-09-27 00:39:00
我设法解决了这个问题。首先,我做了这样的测试:
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.上面的代码将过滤类别分类的页面。
https://stackoverflow.com/questions/52489873
复制相似问题