Order an array with Jekyll / liquid template

核能气质少年 提交于 2019-12-01 14:59:10

问题


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.


回答1:


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 %}



回答2:


simply add reversed in the for statement

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



回答3:


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 (^___^)



来源:https://stackoverflow.com/questions/9218769/order-an-array-with-jekyll-liquid-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!