在Jekyll中的嵌套数据树中循环唯一值

在Jekyll中的嵌套数据树中循环唯一值,jekyll,liquid,Jekyll,Liquid,我有一个包含不同项目的数据文件。每个项都有嵌套的任务。我试图循环嵌套的任务,并按任务类型显示每个任务 YML数据 - name: Outside description: Description tasks: - type: Food name: Eat it outside status: working - type: Drinks name: Drink it outside status: working - name: Inside

我有一个包含不同项目的数据文件。每个项都有嵌套的任务。我试图循环嵌套的任务,并按任务类型显示每个任务

YML数据

- name: Outside
  description: Description
  tasks:
  - type: Food
    name: Eat it outside
    status: working
  - type: Drinks
    name: Drink it outside
    status: working
- name: Inside
  description: Description
  tasks:
  - type: Food
    name: Eat it inside
    status: pending
  - type: Drinks
    name: Drink it inside
    status: working
液体

{% for item in site.data.info %}
    {% assign grouped-tasks-by-type = item.tasks | group_by: "type" %}
    {% for task in grouped-tasks-by-type %}
    <h2 class="task-type">{{ task.type }}</h2>
        <ul>
        {% for task in item.tasks %}
            {% if task.status == 'working' %}
                <li>{{ item.name }}: {{ task.name }}</li>
            {% endif %}
        {% endfor %}
        </ul>
    {% endfor %}
{% endfor %}
{%用于site.data.info%中的项目]
{%assign group tasks by type=item.tasks | group_by:“type”%}
{%对于按类型%分组的任务中的任务}
{{task.type}
    {item.tasks%中任务的%s} {%if task.status=='正在工作'%}
  • {{item.name}}:{{task.name}
  • {%endif%} {%endfor%}
{%endfor%} {%endfor%}
预期结果(HTML)

食物
  • 在外面吃:在外面吃
饮料
  • 外面:在外面喝
  • 里面:在里面喝

然而,我得到了一个完全空白的结果。这是否可能与
分组方法有关?

我希望我下面的算法能很好地为您服务。我能用这个算法达到你期望的最终结果。在我的解决方案中,我无法使用
group_by
。示例代码包含液体注释块,用于解释我的思维过程

测试 我使用了,with命令
jekyll s
。我在本地克隆的minima git repo中将您的YML数据放在一个路径为
\u data/info.YML
的文件中。我使用
post.html
作为代码沙盒

您可以通过在代码中执行
{{all_food_types | json}
将液体变量打印到DOM

解决方案
{%-comment-%}
最终目标:找到适合你的所有食物类型。
1.使用map:“tasks”将所有任务收集到一个数组中。
这将导致每个任务的外部/内部信息丢失
2.使用映射:“类型”创建食物类型列表(“食物”、“饮料”)
3.使用uniq从数组中删除重复的食物类型
{%-endcomment-%}
{%assign all_food_types=site.data.info | map:“tasks”| map:“type”| uniq%}
{%-comment-%}
最终目标:循环查看所有数据
针对特定食品类型(食品、饮料)
并将它们分组
{%-endcomment-%}
{所有食物类型中食物类型的百分比%}
{{food_type}}
    {site.data.info%中项目的%s} {item.tasks%中任务的%s} {%if task.status='working'和task.type==food_type%}
  • {{item.name}}:{{task.name}
  • {%endif%} {%endfor%} {%endfor%}
{%endfor%}

另外,不管出于什么原因,我的液体最终使用了很多数组和for循环。

我在我的Jekyll项目上运行了一个测试,它运行得非常好。伟大的
<h2 class="task-type">Food</h2>
    <ul>
            <li>Outside: Eat it outside<li>
    </ul>
<h2 class="task-type">Drinks</h2>
    <ul>
            <li>Outside: Drink it outside<li>
            <li>Inside: Drink it inside<li>
    </ul>
{%- comment -%} 
  End Goal: Find all the food types for <h2>.

  1.  Use map: "tasks" to gather all the tasks into a single array.
      This will cause the loss of Outside/Inside information for each task

  2.  Use map: "type" to create a list of food types ("Food", "Drinks")
  3.  Use uniq to remove duplicate food types from the array
{%- endcomment -%}

{% assign all_food_types = site.data.info | map: "tasks" | map: "type" | uniq %}

{%- comment -%}
  End Goal: Loop through all the data looking 
            for a specific food type (Food, Drinks) 
            and group them together
{%- endcomment -%}

{% for food_type in all_food_types %}
<h2 class="task-type">{{ food_type }}</h2>
    <ul>
      {% for item in site.data.info %}
        {% for task in item.tasks %}
            {% if task.status == 'working' and task.type == food_type %}
                <li>{{ item.name }}: {{ task.name }}</li>
            {% endif %}
        {% endfor %}
      {% endfor %}
    </ul>
{% endfor %}