简体   繁体   中英

How to sort YAML using Jekyll Liquid

I have the following YML code that I am trying to sort alphabetically in Jekyll:

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

Here is my template code:

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

What do I need to add to the for loop to get the builtWith items to sort alphabetically ?

Thanks!

Try this

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

In the latest Jekyll version, using just sort tag doesn't work because you need to assign it to a variable first: Liquid Warning: Liquid syntax error (line 24): Expected end_of_string but found pipe in "item in page.builtWith | sort" .

If you are not using the latest version then it can work adding sort in the same line.

Using assign and sort tags is safer:

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

Outputs:

Built With

    Brick
    Concrete
    Dirt
    Glass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM