数据/ YAML循环不在Jekyll中循环

时间:2017-03-15 15:13:28

标签: loops jekyll liquid

我的Jekyll项目中有一个名为gallery.yml的文件夹中有一个名为/_data的页面,我想循环遍历gallery.yml中的组并输出一个新的/_includes中的页面。

gallery.yml

- name: Banana Shortcake
  color: yellow
  weight: 2 lbs

- name: Chocolate Mousse
  color: brown
  weight: 9 lbs
  rogue-item: Yum!

- name: Strawberry Fluff
  color: pink
  weight: 0.5 oz

.html中的/_includes页面:

{% for gallery in site.data.gallery %}
<h1>{{ gallery.name }}</h1><!-- This should be Banana Shortcake -->
<p>The color of {{ gallery.name }} is {{ gallery.color }}, and its weight is {{ gallery.weight }}</p>

<h1>{{ gallery.name }}</h1><!-- This should be Chocolate Mousse -->
<p>The color of {{ gallery.name }} is {{ gallery.color }}, and its weight is {{ gallery.weight }}</p>
<p>Rating: {{ gallery.rogue-item }}</p>

<h1>{{ gallery.name }}</h1><!-- This should be Strawberry Fluff -->
<p>The color of {{ gallery.name }} is {{ gallery.color }}, and its weight is {{ gallery.weight }}</p>
{% endfor %}

但是当我应用它时,没有循环 - 它只是重复第一个分组。关于我哪里出错的任何暗示?

1 个答案:

答案 0 :(得分:1)

for标记用于遍历数组并返回数组中的每个项目。

{% for gallery in site.data.gallery %}
  <h1>{{ gallery.name }}</h1><!-- This should be Banana Shortcake -->
  <p>The color of {{ gallery.name }} is {{ gallery.color }}, and its weight is {{ gallery.weight }}</p>
{% endfor %}

将自动运行3次以根据需要输出HTML。