遍历 jekyll 集合

looping over jekyll collections

我有以下代码

<div>
 {% for note in site.regnotes %}
   {% if note.regulationno == page.regulationno %}
     <p>
      {{ note.regulationno }} - {{ note.url }}
     </p>
   {% endif %}
 {% endfor %}
</div>

此代码循环遍历 jekyll 站点中的 regnotes 集合,检查当前注释 regulationno 是否与页面 regulationno 相同,如果是则显示 regulationno 和 url - 即 url 当前页面。如何更改此代码以包含上一页、当前页和下一页的 url。我正在寻找三个 urls - 上一个、当前和下一个? - jekyll 中的 "page.previous.url" 变量似乎在集合中不起作用。

这是其他代码中的样子

for i=1 to number of items in the regnotes collection
  if current note == page note
    print page[i].url        //current page url
    print page[i-1].url      //previous page url
    print page[i+1].url      //next page url
  end if
end for

我想我要做的是通过数组索引引用集合中的项目。似乎无法使语法正确。

既然你是程序员,你只需要知道你需要使用forloop.index0就可以知道你在for循环中的位置(https://docs.shopify.com/themes/liquid-documentation/objects/for-loops#index0)。

代码将类似于:

<div>
 {% for note in site.regnotes %}
   {% assign current_index = forloop.index0 }}
   {% assign next_index = current_index | plus: 1 %}
   {% assign prev_index = current_index | minus: 1 %}
   {% if note.regulationno == page.regulationno %}
     <p>
      {{ note.regulationno }} - {{ note.url }}
     </p>
     {% if site.regnotes[prev_index] %}<a href="{{ site.regnotes[prev_index].url }}">prev</a>{% endif %}
     {% if site.regnotes[next_index] %}<a href="{{ site.regnotes[next_index].url }}">next</a>{% endif %}
   {% endif %}
 {% endfor %}
</div>