我的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 %}
但是当我应用它时,没有循环 - 它只是重复第一个分组。关于我哪里出错的任何暗示?
答案 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。