如何使用 Liquid 模板系统访问非当前 for-loop 元素
How to access non current for-loop elements using the Liquid template system
我正在尝试访问上一个和下一个数组中的元素。 liquid/Jekyll 这甚至可能吗?我想访问上一页和下一页 url。
这就是我到目前为止所做的。
提前致谢。
为了确保该液体在我的网站上正常工作,我通过输出当前页面进行了测试 url :
{{venue.url}}
//this line also works
{{page.url}}
这就是我如何反抗从 yml 文件中获取数据的数组
{% assign page_venue = site.data.venues-array | where: "venueID", page.venue | first %}
这是 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 有效):
当前url:polydeli
上一个 url: 偏红
下一个 url: myrons
我尝试了以下方法,但 none 有效:
<p>{{page.next.url}}</p>
<p>{{venue.next.url}}</p>
<p>{{paginate.next.url}}</p>
<p>{{paginator.next_page}}</p>
更新:
首先,您需要正确格式化 venues-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:
我正在尝试访问上一个和下一个数组中的元素。 liquid/Jekyll 这甚至可能吗?我想访问上一页和下一页 url。
这就是我到目前为止所做的。
提前致谢。
为了确保该液体在我的网站上正常工作,我通过输出当前页面进行了测试 url :
{{venue.url}}
//this line also works
{{page.url}}
这就是我如何反抗从 yml 文件中获取数据的数组
{% assign page_venue = site.data.venues-array | where: "venueID", page.venue | first %}
这是 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 有效):
当前url:polydeli
上一个 url: 偏红
下一个 url: myrons
我尝试了以下方法,但 none 有效:
<p>{{page.next.url}}</p>
<p>{{venue.next.url}}</p>
<p>{{paginate.next.url}}</p>
<p>{{paginator.next_page}}</p>
更新:
首先,您需要正确格式化 venues-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: