【问题标题】:looping over jekyll collections循环遍历 jekyll 集合
【发布时间】:2015-11-24 22:13:05
【问题描述】:

我有以下代码

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

此代码循环遍历 jekyll 站点中的 regnotes 集合,检查当前注释规则是否与页面规则号相同,如果是,则显示规则号和 url - 即当前页面的 url。如何更改此代码以包含上一页、当前页和下一页的 url。我正在寻找三个网址 - 上一个、当前和下一个? - 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

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

【问题讨论】:

  • 我不清楚您在寻找什么。您所在页面的 URL 位于 page.url。至于下一页和上一页,Jekyll 的内置分页只适用于帖子。

标签: loops jekyll liquid


【解决方案1】:

由于您是程序员,您只需要知道您需要使用 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2016-07-16
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多