如何使用Liquid模板系统访问非当前for回路元素

时间:2018-07-12 19:58:43

标签: jekyll liquid liquid-layout

我正在尝试访问上一个和下一个数组中的元素。液体/ Jekyll甚至可能吗?我想访问页面的上一个和下一个URL。 这是我到目前为止所做的。
提前谢谢。

通过输出当前页面的网址,以确保该液体在我测试过的网站上正常工作:

<mat-slider min="0" max="2" step="1" value="2" (change)="changeCar(id)" id="car{{id}}"></mat-slider>
<mat-slider min="0" max="2" step="1" value="0" (change)="changeCar(id)" id="car{{id}}"></mat-slider>

这就是我如何定义从yml文件中获取数据的数组

{{venue.url}}
//this line also works
{{page.url}}

这是yml文件的一部分:

{% assign page_venue = site.data.venues-array | where: "venueID",   page.venue | first %}

所以我在输出这样的内容时遇到麻烦(仅当前网址有效):

当前网址: polydeli
上一个网址:带红色
下一个网址: myrons

我尝试了以下方法,但是没有用:

venueID: Red-Radish
name: Red Radish
url: redradish
building: 65
neighborhood: University Union

venueID: Poly-Deli
name: Poly Deli
url: polydeli
building: 19
neighborhood: University Union

venueID: Myrons
name: Myron's
url: myrons
previous: MustangStation
building: 19
neighborhood: University Union

1 个答案:

答案 0 :(得分:1)

已更新:

首先,您需要正确格式化场所文件-array.yml,如下所示:

- venueID: Red-Radish
  name: Red Radish
  url: redradish
  building: 65
  neighborhood: University Union

- venueID: Poly-Deli
  name: Poly Deli
  url: polydeli
  building: 19
  neighborhood: University Union

- venueID: Myrons
  name: Myron's
  url: myrons
  previous: MustangStation
  building: 19
  neighborhood: University Union

然后您可以访问下一个和上一个URL,如下所示:

{% for venue in site.data.venues-array %}

  {% assign next = forloop.index0 | plus: 1 %}
  {% assign previous = forloop.index0 | minus: 1 %}
    <div>Name: {{ venue.name }}</div>
    <div>Current URL: {{ venue.url }}</div>

    <div>Previous url:{{ site.data.venues-array[previous].url }}</div>
    <div>Next URL is:{{ site.data.venues-array[next].url }}</div>
    <hr>

{% endfor %}

哪个会退出:

Name: Red Radish
Current URL: redradish
Previous url:myrons
Next URL is:polydeli
Name: Poly Deli
Current URL: polydeli
Previous url:redradish
Next URL is:myrons
Name: Myron's
Current URL: myrons
Previous url:polydeli
Next URL is: