如何连接 liquid 标签内迭代器的值 (Jekyll)

How to concatenate the value of iterator inside liquid tag (Jekyll)

我正在遍历 Yaml 对象,需要将 'i' 的值附加到正确对象的地址。

<div class="chapter">
  {% for i in (1..7) %}
    <strong class="title">{{ page.chapters.title + i }}</strong>
    <ul>
      {% for topic in page.chapters.ch + i %}
        <li>
          {{ topic }}
        </li>
      {% endfor %}
    </ul>
  {% endfor %}
</div>

数据:

---
layout: default
title: Home
chapters:
  title1: "CHAPTER 1: LEADERSHIP"
  ch1:
    - Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    - Aliquam tincidunt mauris eu risus.
    - Vestibulum auctor dapibus neque.

  title2: "CHAPTER 2: THE EXPERIENCE"
  ch2:
    - Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    - Aliquam tincidunt mauris eu risus.
    - Vestibulum auctor dapibus neque.
---

不起作用的部分是:

{% for topic in page.chapters.ch + i %}

我需要 ch 的值是 ch1、ch2 等...

您可以将 YAML 作为 Ruby 关联数组访问,但使用 + 运算符的内联操作将不起作用。您可以使用 append 过滤器来补救。

试试这个:

{% assign ch_key = 'ch' | append: i %}
{% for topic in page.chapters[ch_key] %}
    <li>{{ topic }}</li>
{% endfor %}

我通过更新数据模型和创建嵌套循环来完成这项工作:

---
chapters:
  - title: "CHAPTER 1: LEADERSHIP"
   chapterList:
     - Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
     - Aliquam tincidunt mauris eu risus.
     - Vestibulum auctor dapibus neque.

 - title: "CHAPTER 2: THE EXPERIENCE"
   chapterList:
     - Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
     - Aliquam tincidunt mauris eu risus.
     - Vestibulum auctor dapibus neque.
---
<div class="chapter">
  {% for chapter in page.chapters %}
   <strong class="title">{{ chapter.title }}</strong>
     <ul>
        {% for chapterItem in chapter.chapterList %}
         <li>
            {{ chapterItem }}
         </li>
         {% endfor %}
     </ul>
  {% endfor %}
</div>