2017-01-29 31 views
1
排序YAML

我有我試圖在傑基爾排序alphabetically以下YML代碼:如何使用傑基爾液體

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

謝謝!

回答

1

試試這個

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

,完美的工作,謝謝! –

+0

只需注意括號'('')'在Liquid中不做任何事情,你可以從這段代碼中刪除它們。 –

0

在最新版本的傑基爾,只用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