将变量传递到for循环中-不起作用

时间:2019-07-06 21:19:02

标签: jekyll

我正试图在for循环中将参数传递给液体过滤器,但无法使其正常工作。

_data中,我有一个名为fruit_metadata.yml的文件,其内容如下:

apples:
  country: unites states

bananas:
  country: brazil

oranges:
  country: florida

我想遍历内容并获取每种水果的国家。这是我在做什么:


{% assign fruit = "apples, oranges, bananas" | split: "," %}
{% for item in fruit %}
* doesn't work: {{ site.data.fruit_metadata[item].country }}
* works: {{ site.data.fruit_metadata.apples.country }}
{% endfor %}```

For some reason, `[item]` doesn't seem to be working. The "doesn't work" line returns nothing. The "works" line returns "united states."

How can I pass in `item` here to get the data?

1 个答案:

答案 0 :(得分:0)

原来,我只需要删除apples,oranges,bananas中的空格。