【问题标题】:Jeykll: Sorting files in _data subfolders by common propertyJekyll:按公共属性对数据子文件夹中的文件进行排序
【发布时间】:2015-03-19 11:01:20
【问题描述】:

我想遍历_data/sections/ 中的每个文件,但输出按所述文件中包含的数据排序(order 属性)。当前输出的顺序恰好是正确的,虽然我不知道为什么,并且在修改 sorted 属性时顺序确实没有改变。

文件结构如下:

// project/_data/sections/food.yml

title: Food
order: 2
content: "Food ipsum dolor sit amet."

-----

// project/_data/sections/drink.yml

title: Drink
order: 1
content: "Drink ipsum dolor sit amet."

按照Jekyll docs for data files上的结构,for循环代码如下:

// project/index.html

// ...
{% for section_hash in site.data.sections | sort: 'order' %}
  {% assign section = section_hash[1] %}
  <p><strong>{{ section.title }}</strong> - {{ section.content }}</p>
{% endfor %}
// ...

我还尝试在将这些部分传递给 for 循环 as seen here 之前对它们进行排序:

{% assign sections_sorted = sita.data.sections | sort: 'order' %}
{% for section in sections_sorted %}
  <p><strong>{{ section.title }}</strong> - {{ section.content }}</p>
{% endfor %}

最后,我尝试将order属性移动到_data/sections/中每个节文件的front-matter,但这导致了异常:Liquid Exception: no implicit conversion of String into Integer

// project/_data/sections/drink.yml
---
order: 1
---

title: Drink
content: "Drink ipsum dolor sit amet."

这对于_data/ 子目录中的文件是否可行?如何按order 对这些文件的输出进行数字排序,按title 反向字母排序,等等?

【问题讨论】:

    标签: sorting for-loop jekyll liquid


    【解决方案1】:

    刚刚遇到同样的问题。 site.data.whateverfor 文件夹总是哈希

    { file_name => file_content, ... }
    

    很遗憾,Liquid 数组过滤器不支持哈希。要将其转换为数组,可以使用以下过滤器:

    module Jekyll
      module ValuesFilter
        def values(input)
          case
            when input.instance_of?(Hash)
              input.values
            else
              input
          end
        end
      end
    end
    
    Liquid::Template.register_filter(Jekyll::ValuesFilter) 
    

    将其放入_plugins 文件夹并使用下一个 Liquid 代码:

    {% assign ordered_items =(site.data.folder_name | sort | order: 'field') %}
    {% for item in ordered_items %}
        {{ item.<property_name> }}
    {% endfor %}
    

    如果folder_name 指向 CSV 文件或 JSON 文件中的数组,此代码也有效。

    【讨论】:

    • 在最终通话中应该是folder_name | values | sort | order: 'field'
    【解决方案2】:

    您的第二个示例应该可以工作,但是您有一个错字:sita.data.sections 应该是 site.data.sections

    {% assign sections_sorted = site.data.sections | sort: 'order' %}
    {% for section in sections_sorted %}
      <p><strong>{{ section.title }}</strong> - {{ section.content }}</p>
    {% endfor %}
    

    【讨论】:

      【解决方案3】:

      我不确定我的问题是否与您的问题有关。我正在尝试对 _data 目录中的文件进行排序,但我的文件是 JSON 格式。无论我做什么,我都会收到此错误消息:jekyll 2.5.3 | Error: no implicit conversion of String into Integer。我的 JSON 文件根本不包含任何前端内容。错误消息告诉我 Ruby 尝试使用字符串访问数组,但出于任何原因需要一个整数,例如my_array["blah"] 而不是 my_array[1]。这没有多大意义,因为sort:"blah" 实际上提供了一个字符串。你能解决这个问题吗?

      关于你的第一个例子:
      {% for section_hash in site.data.sections | sort: 'order' %}
      我认为这行不通,我相信这在 Liquid 中没有实现。看到这个问题:https://github.com/Shopify/liquid/pull/304

      但我不明白为什么你的第二个例子不起作用,也许我在这里误解了一些东西:
      {% assign sections_sorted = sita.data.sections | sort: 'order' %}
      显然,这应该从 Jekyll 2.2.0 开始工作,请参阅此线程:Sorted navigation menu with Jekyll and Liquid

      【讨论】:

        【解决方案4】:

        即使这已经过时了,我也遇到了同样的问题。 我的解决方案是提供一个 _index.xml 来定义例如的顺序。项目。

        我们在这个文件夹中:_data/projects

        有很多文件,例如

        • _index.yml
        • project_1.yml
        • project_2.yml
        • project_3.yml

        _index.yml 的内容是这样的:

        - project_2
        - project_1
        - project_3
        

        显示您的项目的调用如下所示:

        {% for project_id in site.data.projects["_index"] %}
        {% assign project = site.data.projects[project_id] %} 
          // do something with the project
        {% endfor %}
        

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 2011-02-16
          • 1970-01-01
          • 2023-04-11
          • 1970-01-01
          • 1970-01-01
          • 2013-02-15
          • 1970-01-01
          • 1970-01-01
          • 2014-06-08
          相关资源
          最近更新 更多