2

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>
Keith Mifsud
  • 1,599
  • 1
  • 16
  • 26
Charlie805
  • 169
  • 1
  • 11

1 Answers1

1

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:
Keith Mifsud
  • 1,599
  • 1
  • 16
  • 26
  • Thanks, but for some reason {{ previous.url }} and {{ next.url }} are not working. – Charlie805 Jul 18 '18 at 18:37
  • Hi Charlie, sorry I didn't test it before. The updated answer is tested and works a treat :) – Keith Mifsud Jul 19 '18 at 01:45
  • Thanks Keith! With that code the whole list of restaurants outputs. Do you know what i could so so i only ouput one restaurant at a time. like this: Name: Red Radish Current URL: redradish Previous url:myrons Next URL is:polydeli – Charlie805 Jul 20 '18 at 18:08
  • 1
    Hi, I think you want to paginate the venues. Am I correct to say that you want a page per venue, the page will include one Venue information and links to the previous and next venue? If so you will either need to use a pagination plugin or quickly hack it with javascript. In any case, please accept this answer as it answers your question (accessing the for loop elements) and feel free to send me a link to the pagination question and I'll answer that one in full detail. – Keith Mifsud Jul 21 '18 at 04:13
  • Hi Keith, here the link https://stackoverflow.com/questions/51488205/how-to-paginate-through-arrays-with-liquid Thanks! – Charlie805 Jul 23 '18 at 22:55