循环可以在Jekyll中包含另一个循环吗?

时间:2017-07-24 17:44:53

标签: markdown jekyll

我有一个主要部分,其中包含一个<h2>和多个段落。我想为每个<p>显示一个新段落。有没有办法在这个内部内容部分中有另一个for循环,所以只要有描述就会返回新的段落?

这是带有液体循环的HTML:

<div class="content-inner">
  {% for innercontent in page.innercontent %}
   <h2>{{ innercontent.title }}</h2>
   {% for desc in page.innercontent %}
    <p>{{ innercontent.desc }}</p>
   {% endfor %}
 {% endfor %}
</div>

降价文件:

Section:
  - title: This is the title
    desc: This is the first description paragraph.
    desc: This is the second description paragraph.

1 个答案:

答案 0 :(得分:1)

目前定义的方式,Jekyll将从每个部分看到:

- title: This is the title
  desc: This is the second description paragraph.

因为有两个具有相同名称的序列。

要为每个部分定义多个描述,您可以使用此前端:

---
Section:
  - title: This is the title
    desc: 
      - This is the first description paragraph.
      - This is the second description paragraph.
---

然后使用page.Section访问部分的前端,并使用innercontent.desc循环显示所有描述:

<div class="content-inner">
  {% for innercontent in page.Section %}
   <h2>{{ innercontent.title }}</h2>
   {% for desc in innercontent.desc %}
    <p>{{ desc }}</p>
   {% endfor %}
 {% endfor %}
</div>

那将输出:

<div class="content-inner">

   <h2>This is the title</h2>

    <p>This is the first description paragraph.</p>

    <p>This is the second description paragraph.</p>


</div>