循环遍历Jekyll / YAML中的嵌套图像库数组

时间:2017-03-27 04:23:55

标签: arrays loops yaml jekyll liquid

为什么我不能在Jekyll中为这个项目组的main:图像数组循环?

/_data/navigation.yml:

- project:
  -
    categ: navigation
    name: Letterman
    age: 54
    feeling: swell
    thumb: thumb-letterman.jpg
    main:
      - image: image_1.jpeg

- project:
  -
    categ: navigation
    name: Carlin
    age: 67
    feeling: nice
    thumb: thumb-carlin.jpg
    main:
      - image: image_1.jpeg
      - image: image_2.jpeg
      - image: image_3.jpeg

navigation-page.html:

{% for navigation in site.data.navigation %}

    {% for project in navigation.project %}
        <div style="border:1px solid purple;margin:40px;">
            <p style="font-size:1em;color:purple">{{ project.name }}</p>

            {% for main in navigation.project %}
                <img src="{{ page.path }}{{ image }}" />
            {% endfor %}
        </div>
    {% endfor %}

{% endfor %}

这个输出应该是两个div行,每个行都有项目名称,第一行有一个图像,第二行有三个图像。

1 个答案:

答案 0 :(得分:1)

这是你的循环问题。

foo: Foo

我使用以下内容开始工作。

HTML(带值)

{% for project in navigation.project %}

  {% for image in project.main %}

    <!-- This loop needed to loop within its parent -->

  {% endfor %}

{% endfor %}

HTML(循环索引)

{% for item in site.data.navigation %}
<ul>
  <li>
    <p style="border: solid 1px red;">Project loop {{ forloop.index }}</p>
    {% for project in item.project %}
    <ul>
      <li>
        <p style="border: solid 1px green;">Sub Loop {{ forloop.index }}</p>
        <ul>
          <li style="border: solid 1px blue;">
            <p><strong>categ:-</strong> {{project.categ}}</p>
          </li>
          <li style="border: solid 1px blue;">
            <p><strong>name:-</strong> {{project.name}}</p>
          </li>
          <li style="border: solid 1px blue;">
            <p><strong>age:-</strong> {{project.age}}</p>
          </li>
          <li style="border: solid 1px blue;">
            <p><strong>feeling:-</strong> {{project.feeling}}</p>
          </li>
          <li style="border: solid 1px blue;">
            <p><strong>thumbnail:-</strong> {{project.thumb}}</p>
          </li>
          <li>
            <ol>
              {% for image in project.main %}
                <li style="border: solid 1px purple;">
                  <p>{{ image.image }}</p>              
                </li>          
              {% endfor %}
            </ol>            
          </li>
        </ul>
      </li>
    </ul>
    {% endfor %}
  </li>
</ul>
{% endfor %}

<强> YAML

<div style="border: solid 4px purple; padding: 10px;">
  {% for item in site.data.navigation %}
    <div style="border: solid 3px red; padding: 10px; margin: 10px;">
      <pre>LOOP {{ forloop.index }}</pre>
      {% for project in item.project %}
        <div style="border: solid 2px green; padding: 10px; margin: 10px;">
          <pre>SUB LOOP {{ forloop.index }}</pre>
          {% for image in project.main %}
            <div style="border: solid 1px blue; padding: 10px; margin: 10px;">
              <pre>IMAGE {{ forloop.index }}</pre>
            </div>
          {% endfor %}
        </div>
      {% endfor %}
    </div>
  {% endfor %}
</div>