在Liquid中对集合进行过滤或分组 [英] Filter or group a collection in Liquid

查看:67
本文介绍了在Liquid中对集合进行过滤或分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个简单的个人Jekyll博客,我想通过post上的属性lang(语言)对我的site.posts进行分组.这是"en","nl"或nil.

For a simple personal Jekyll blog, I want to group my site.posts by an attribute on post, lang (language). This is either "en", "nl" or nil.

然后,我想呈现两个帖子列表.目前,我有:

I then want to render two lists of posts. Currently I have:

<section lang="nl">
<h2>Nederlandse Artikelen</h2>
<ul class="posts">
  {% for post in site.posts limit:50 %}
    {% if post.lang == "nl" %}
      {% include li_for_post_with_date.yml %}
    {% endif %}
  {% endfor %}
</ul>
<a href="archief.html">Archief »</a>
</section>
<section lang="en">
<h2>English Articles</h2>
<ul class="posts">
  {% for post in site.posts limit:50 %}
    {% if post.lang == nil or post.lang == "en" %}
      <li><span>{{ post.date | date_to_string }}</span> &raquo; <a href="{{ BASE_PATH }}{{ post.url }}">{{ post.title }}</a></li>
    {% endif %}
  {% endfor %}
</ul>

这有两个问题:

  1. 最烦人的;当最近50个帖子中有47个lang = en和3个lang = nl时,我现在得到了一个倾斜的列表.我想要25个lang = en和25个lang = nl条目.
  2. 循环走了两次,这使我感到无能为力.

是否可以在Liquid中分配或准备收藏?这样,我可以循环遍历site.posts并准备一个像site.grouped_posts[en]这样的嵌套集合.

Is there a way to assign or prepare a collection in Liquid? That way I could loop over site.posts once and prepare a nested collection like site.grouped_posts[en].

还是另一把戏?

解决方案

汤姆·克拉克森(Tom Clarkson)指出,保持对立是正确的方向.但是,仅在最近的Liquid版本中才增加了一个计数器,在Github(我的液体被编译的地方)上运行的那个有2.2.2,而没有增加计数器的能力. Toms解决方案本身也不起作用,因为Liquid将变量counter转换为字符串,无法与<进行比较.

As Tom Clarkson points out, maintaining a counter is the right direction. However, incrementing a counter has only landed in recent Liquid versions, the one running on Github (where my liquid is compiled) has 2.2.2, without ability to increment a counter. Toms solution itself is not working either, because Liquid turns the variable counter into a string, which cannot be compared with <.

我通过添加一个字符串并计算字符数来创建了一个hack.

I created a hack, by appending a string and counting the characters.

{% assign counter = '.' %}
{% for post in site.posts %}
  {% if counter.size <= 25 and post.lang == "nl" %}
    {% capture counter %}{{ counter | append:'.' }}{% endcapture %}
    {% include li_for_post_with_date.yml %}
  {% endif %}
{% endfor %}

如上所述,这很丑陋,因此,如果有更清洁的解决方案,请添加解决方案!

As said, ugly, so if there are cleaner solutions, please add a solution!

推荐答案

我认为您不创建插件或自定义过滤器就无法创建过滤后的集合,但是您可能能够计算已针对该集合收集的帖子数而不是使用限制.

I don't think you can create the filtered collection without making a plugin or custom filter, but you may be able to count the number of posts already collected for the group rather than using limit.

{% for post in site.posts %}
    {% if counter < 25 and post.lang == nil or post.lang == "en" %}
        {% capture counter %}{{ counter | plus:1 }}{% endcapture %} 
        <li></li>
    {% endif %}
{% endfor %}

该代码未经测试,但是类似的东西应该可以工作.

The code is untested, but something fairly similar should work.

这篇关于在Liquid中对集合进行过滤或分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆