Jekyll 存档页面中的嵌套液体循环不起作用。在内部条件中使用外部循环变量
我正在使用 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">↩</a>
{% endfor %}
我的 jekyll 网站中有一个名为“测试”的帖子类别,我可以 显示其中的帖子,内容如下:
{% for post in site.categories.test %}
<li> <a href="{{ post.url }}"> {{ post.title }}</a></li>
{% endfor %}
但是,我想自动构建一个存档页面,并且按顺序 为此,我需要从外循环(循环 访问所有类别),并在内循环中使用它 访问该特定类别的帖子。我需要做什么才能获得 第一个片段按照我想要的方式工作?
编辑:或者,还有另一种方法可以获得我想要的结果吗?
I am working with the jekyll static site builder, and I am having
difficulty performing the following:
{% 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">↩</a>
{% endfor %}
I have a post category in my jekyll site called 'test', and I can
display posts from it with the following:
{% for post in site.categories.test %}
<li> <a href="{{ post.url }}"> {{ post.title }}</a></li>
{% endfor %}
However, i want to build an archive page automatically, and in order
to do this, I need to embed the category from the outer loop (the loop
that visits all the categories), and use it inside the inner loop to
access posts from that specific category. What do I need to do to get
the first snippet to work how I want it to?
EDIT: Alternatively, is there another way to get the results that I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样...
当然,它的效率非常低,并且会生成一堆额外的空白,但它可以完成工作。
[原件缺少标签。刚刚添加了它们。另外,为了摆脱空白,可以将从
for post in site.posts
到endfor
的所有内容折叠到一行中。]How about...
Sure, it's pretty inefficient and generates a bunch of extra whitespace, but it gets the job done.
[The original was missing the tags. Just added them. Also, to get ride of the whitespace, one can collapse everything from
for post in site.posts
toendfor
onto a single line.]另外,我不确定为什么 kshep 的示例不起作用......
Also, I'm not sure why kshep's example doesn't work...
当您执行
for site.categories
中的类别时,category[0]
将为您提供类别名称,category[1]
将为您提供该类别的帖子列表。我相信这就是 Liquid 处理哈希迭代的方式。
因此,您正在寻找的代码是这样的:
我冒昧地修复了一些标记问题 - 我在帖子链接周围添加了
...
列表,最后一个链接周围有一个
,
8617
之后有一个分号,并且还将id
固定在顶部(缺少-ref
部分)。问候!
When you do
for category in site.categories
,category[0]
will give you the category name,category[1]
will give you the list of posts for that category.That's the way Liquid handles iteration over hashes, I believe.
So the code you are looking for is this one:
I've taken the liberty of fixing some markup issues - I've added
<ul>...</ul>
around the post link list, a<p>
around the last link, a semi-colon after the8617
, and also fixed theid
at the top (was missing the-ref
part).Regards!