【问题标题】:How to sort YAML using Jekyll Liquid如何使用 Jekyll Liquid 对 YAML 进行排序
【发布时间】:2017-06-14 11:40:04
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: loops sorting github yaml jekyll


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 请注意,括号 ( ) 在 Liquid 中没有任何作用,您可以从这个 sn-p 中删除它们。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2018-12-09
      • 2018-12-25
      • 2011-11-08
      • 2015-11-09
      • 1970-01-01
      相关资源
      最近更新 更多