如何使用Jekyll Liquid对YAML进行分类

时间:2017-01-29 01:52:13

标签: loops sorting github yaml jekyll

我有以下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进行排序?

谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

在最新的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