jekyllliquidliquid-layout

How to access non current for-loop elements using the Liquid template system


I'm trying to access elements from the previous and next arrays. Is this even possible with liquid/Jekyll? I want to access the previous and next url of pages. This is what I have done so far.
Thanks in advance.

To make sure that that liquid is working on my site I tested by outputting the current page url :

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

and this is how I defied the array which takes the data from a yml file

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

This is part of the yml file:

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

So I'm having trouble outputting something like this(only the current url works):

Current url: polydeli
Previous url: reddish
Next url: myrons

I've the tried the following, but none work:

<p>{{page.next.url}}</p>
<p>{{venue.next.url}}</p>
<p>{{paginate.next.url}}</p>
<p>{{paginator.next_page}}</p>

Solution

  • Updated:

    First, you need to format the venues-array.yml file correctly, like so:

    - 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
    

    Then you can access the next and previous URLs like this:

    {% 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 %}
    

    Which will out this:

    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: