如何使用 Jekyll Liquid 对 YAML 进行排序

How to sort YAML using Jekyll Liquid

我有以下 YML 代码,我试图在 Jekyll 中对 alphabetically 进行排序:

layout: project
title: Home renovation
link: http://urlgoeshere.com    
builtWith:
  - Concrete
  - Glass
  - Brick
  - Dirt

这是我的模板代码:

  <h4>Built With</h4>
    <ul class="list-unstyled list-inline list-responsibilities">
      {% for item in page.builtWith %}
        <li>{{ item }}</li>
      {% endfor %}
    </ul>

我需要在 for 循环中添加什么才能让 builtWith 项排序 alphabetically

谢谢!

试试这个

{% assign sorted = (page.builtWith | sort) %}
{% for item in sorted %}

在最新的 Jekyll 版本中,仅使用 sort 标签是行不通的,因为您需要先将其分配给一个变量:Liquid Warning: Liquid syntax error (line 24): Expected end_of_string but found pipe in "item in page.builtWith | sort"

如果您使用的不是最新版本,则可以在同一行中添加 sort

使用 assignsort 标签更安全:

<h4>Built With</h4>
<ul class="list-unstyled list-inline list-responsibilities">
{% assign sorted = page.builtWith | sort %}
{% for item in sorted %}
<li>{{ item }}</li>
{% endfor %}
</ul>

输出:

Built With

    Brick
    Concrete
    Dirt
    Glass