使用 Jekyll / Liquid 模板订购阵列

发布于 2025-01-04 07:39:59 字数 880 浏览 5 评论 0原文

我正在尝试执行以下操作。 我使用 Jekyll 创建帖子列表,并按类别排序(周一...周日) 我想让它们按时间顺序显示,但 Jekyll 按字母顺序排列。

是否可以使用 Jekyll 对 arry 进行排序?

我已在 post yaml 中添加了一个订单键以镜像 monday = 1 ... sunday = 7

我尝试使用此订单键对数组进行排序,但它不起作用。

  {% for post in posts_collate  %}
    {% capture class %} {{ post.tags | first }} {% endcapture%}
    {% capture club %} {{ post.tags | last }} {% endcapture%}

    {% if forloop.first %}
      <h2>our events</h2>
      <h3>{{ class }} & {{ club }}</h3>
      <dl>
    {% endif %}
    {% if post.rel == 'me' %}
      <dt>{{ post.category | sort: 'order' }}</dt> 
      <dd> <a href="{{ BASE_PATH }}{{ post.url }}">{{ post.title }}</a></dd>
    {% endif %}

    {% if forloop.last %}
      </dl>
    {% endif %}
  {% endfor %}

我在大型谷歌机器中找不到任何信息,所以我不确定这是否可能。

I'm trying to do the following.
I use Jekyll to create a list of posts, and ordertem by category (monday...sunday)
I'd like to have them displayed in chronological order but Jekyll order them alphabetically.

Is it possible to sort an arry with Jekyll?

I have added an order key to the post yaml to mirror monday = 1 ... sunday = 7

I'm trying to then sort the array with this order key, bu it doesn't work.

  {% for post in posts_collate  %}
    {% capture class %} {{ post.tags | first }} {% endcapture%}
    {% capture club %} {{ post.tags | last }} {% endcapture%}

    {% if forloop.first %}
      <h2>our events</h2>
      <h3>{{ class }} & {{ club }}</h3>
      <dl>
    {% endif %}
    {% if post.rel == 'me' %}
      <dt>{{ post.category | sort: 'order' }}</dt> 
      <dd> <a href="{{ BASE_PATH }}{{ post.url }}">{{ post.title }}</a></dd>
    {% endif %}

    {% if forloop.last %}
      </dl>
    {% endif %}
  {% endfor %}

And i can't find any info in the big google machine, so i'm not sure thay it is possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

变身佩奇 2025-01-11 07:39:59

如果没有插件或自定义功能,这是无法完成的。尽管如此,我们仍在努力在下一个版本中实现这一点:https://github.com /Shopify/liquid/pull/101 然后它看起来像:

{% for tag in site.tags order:ascending %} 
   ...
{% endfor %}

It cannot be done without a plugin or custom function. Although, there is an ongoing effort to implement this in the next releases: https://github.com/Shopify/liquid/pull/101 and then it would look like:

{% for tag in site.tags order:ascending %} 
   ...
{% endfor %}
流年已逝 2025-01-11 07:39:59

只需在 for 语句中添加 reversed 即可

 {% for post in site.posts reversed %}
   ...
 {% endfor %}

simply add reversed in the for statement

 {% for post in site.posts reversed %}
   ...
 {% endfor %}
┈┾☆殇 2025-01-11 07:39:59

好吧,不确定是否可以进行这种排序,但我有点作弊
(•__°)

我刚刚将我想要订购的帖子重命名为特定日期,例如:

0000-00-01-firstpost
等等...

现在为我做,但如果有更聪明的方法来做到这一点,我愿意(^___^)

Ok, not sure it is possible at all to do this kind of sorting, but i've cheated a bit
(•__°)

I just renamed the post that i want to be order with a specific date like:

0000-00-01-firstpost
etc...

Does it for me for now, but if there is a more clever way to do that, I'm up for it (^___^)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文