8

我正在使用 jekyll 静态站点构建器,但在执行以下操作时遇到了困难:

{% for category in site.categories %} 
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2> 
        {% for post in site.categories[{{ category }}] %} 
                <li> <a href="{{ post.url }}"> {{ post.title }}</a></li> 
        {% endfor %} 
<a href="#{{ category[0] }}-ref">&#8617</a> 
{% endfor %} 

我的 jekyll 网站中有一个名为“test”的帖子类别,我可以使用以下内容显示其中的帖子:

{% for post in site.categories.test %} 
                <li> <a href="{{ post.url }}"> {{ post.title }}</a></li> 
{% endfor %} 

但是,我想自动构建一个存档页面,为了做到这一点,我需要从外部循环(访问所有类别的循环)嵌入类别,并在内部循环中使用它来访问来自该循环的帖子具体类别。我需要做什么才能让第一个片段按我想要的方式工作?

编辑:或者,还有其他方法可以获得我想要的结果吗?

4

3 回答 3

15

当你这样做时for category in site.categories

  • category[0]会给你类别名称,
  • category[1]将为您提供该类别的帖子列表。

我相信,这就是 Liquid 处理哈希迭代的方式。

所以你正在寻找的代码是这个:

{% for category in site.categories %} 
<h2 id="{{ category[0] }}-ref">{{ category[0] }}</h2>
<ul>
  {% for post in category[1] %} 
    <li><a href="{{ post.url }}">{{ post.title }}</a></li> 
  {% endfor %}
</ul>
<p><a href="#{{ category[0] }}-ref">&#8617;</a></p>
{% endfor %}

我冒昧地修复了一些标记问题 - 我<ul>...</ul>在帖子链接列表<p>周围添加了一个,在最后一个链接周围添加了一个,在 之后添加了一个分号8617,并且还修复id了顶部的 (缺少-ref部分)。

问候!

于 2011-04-20T08:15:25.107 回答
1

怎么样...

{% for category in site.categories %}
<h2 id = "{{ category[0] }}"> {{ category[0] }} </h2>
        <ul>
        {% for post in site.posts %}
            {% if post.category == category[0] %}
                 <li> <a href="{{ post.url }}"> {{ post.title }}</a></li>
            {% endif %}
        {% endfor %}
        </ul>
<a href="#{{ category[0] }}-ref">&#8617</a>
{% endfor %}

当然,它的效率非常低,并且会产生一堆额外的空白,但它可以完成工作。

[原件缺少标签。刚刚添加了它们。此外,为了摆脱空白,可以将所有内容从for post in site.posts到折叠endfor到一行。]

于 2011-04-17T16:40:20.097 回答
0
        {% for post in site.categories.category %} 
        - OR -
        {% for post in site.categories.category[0] %} 

另外,我不确定为什么 kshep 的示例不起作用...

于 2011-04-19T15:47:52.667 回答