0
votes

i have a jekyll site and i need to have a gallery on certain pages.

right now, for each page, i have a corresponding file in the _data folder that contains a list of image paths. then, i build the galklery on each page with the following code:

{% for x in site.data.one %}
<img src="{{ x.img }}">
{% endfor %}

this is only for the page called "one".

for the page called "two", i will have similar code:

{% for x in site.data.two %}
<img src="{{ x.img }}">
{% endfor %}

etc.

i was wondering, is it possible to abstract this away into a file in the _includes folder? so i can just call

{% include gallery.html %}

in any page? and then the code detects which page its being called from, and changes it appropriatey as above?

hope i am clear.

1

1 Answers

1
votes

In your _data/galleries.yml

1:
  - thumb: thumb1-1/src.jpg
    medium: medium1-1/src.jpg
  - thumb: thumb1-2/src.jpg
    medium: medium1-2/src.jpg

2:
  - thumb: thumb10-1/src.jpg
    medium: medium10-1/src.jpg
  - thumb: thumb10-2/src.jpg
    medium: medium10-2/src.jpg

In a page with a gallery :

---
layout: default
title: Page title
gallery_data_id: 1  << id of gallery datas for this page
---

<h1>Article 1</h1>

{% include gallery.html %}

In _includes/gallery.html

{% if site.data.links[page.gallery_data_id] %}  << NO DATA = print nothing
    <ul>
    {% for img in site.data.galleries[page.gallery_data_id] %}
        <li>
        <!-- do what you want with your datas here -->
        thumb = {{img['thumb']}} - img = {{img['medium']}}
        </li>
    {% endfor %}
    </ul>
{% endif %}