简体   繁体   English

遍历 Eleventy / 11ty 中的全局数据子文件夹

[英]Iterate over global data subfolder in Eleventy / 11ty

I'm using Eleventy and have created the subfolder yummy in my global data folder, that contains the following files:我正在使用 Eleventy 并在我的全局数据文件夹中创建了子文件夹yummy ,其中包含以下文件:

\src\_data\yummy\drinks.json
\src\_data\yummy\food.json

When I use {{ yummy | dump }}当我使用{{ yummy | dump }} {{ yummy | dump }} I get the following output: {{ yummy | dump }}我得到以下 output:

{"drinks":[{"name":"Milk","price":5},{"name":"Water","price":1}],"food":[{"name":"Pizza","price":4},{"name":"Hot dog","price":2},{"name":"Sallad","price":1},{"name":"Avocado","price":3}]}

I want to create a list with all entries in both json-files.我想创建一个列表,其中包含两个 json 文件中的所有条目。 I almost have achieved this with the following loop:我几乎已经通过以下循环实现了这一点:

<ul>
{% for key, val in yummy %}
<li>{{ val[0].name }} cost {{ val[0].price }}$</li>
{% endfor %}
</ul>

However, since I'm using [0], the output is only the first entry in each json-file:但是,由于我使用的是 [0],因此 output 只是每个 json 文件中的第一个条目:

- Milk cost 5$
- Pizza cost 4$

I want all entries and have tried the following, but get no output at all:我想要所有条目并尝试了以下方法,但根本没有得到 output:

<ul>
{% for key, val in yummy %}
<li>{{ val[key].name }} cost {{ val[key].price }}$</li>
{% endfor %}
</ul>

Managed to solve this myself by adding a second loop.通过添加第二个循环设法自己解决这个问题。

<ul>
  {% for key, val in yummy %}
    {% for entry in val %}
    <li>{{ entry.name }} cost {{ entry.price }}$</li>
    {% endfor %}
  {% endfor %}
</ul>

Outputs as:输出为:

- Milk cost 5$
- Water cost 1$
- Pizza cost 4$
- Hot dog cost 2$
- Sallad cost 1$
- Avocado cost 3$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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