# Use different \_data files in \_include based off page user is on

**URL:** <https://talk.jekyllrb.com/t/use-different-data-files-in-include-based-off-page-user-is-on/8196>\
**Category:** Help\
**Created:** [April 29, 2023, 6:30am UTC](https://talk.jekyllrb.com/t/use-different-data-files-in-include-based-off-page-user-is-on/8196 "2023-04-29T06:30:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parmjot-Singh](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/parmjot-singh/32/5299_2.png) [@Parmjot-Singh](https://talk.jekyllrb.com/u/Parmjot-Singh)\
**Post date:** [April 29, 2023, 6:30am UTC](https://talk.jekyllrb.com/t/use-different-data-files-in-include-based-off-page-user-is-on/8196/1 "2023-04-29T06:30:14Z")

</div>

I was trying to create a feature in my theme where if a user wanted to have some images in a gallery on one of their blog posts, they could create a file in \_data, and then the images would be rendered in their post. This was based off of [Adaptive Photo Layout with Flexbox | CSS-Tricks - CSS-Tricks](https://css-tricks.com/adaptive-photo-layout-with-flexbox/).  
The main issue for me is that I cannot figure out a way to make sure that each blog post doesn’t use the same images.  
I wanted to make it so that the user doesn’t have to put any HTML code in their markdown, so I made a [file in my \_includes folder](https://github.com/Parmjot-Singh/jekyll-theme-dusk/blob/master/_includes/image-masonry.html) (which currently has hardcoded paths to some example images) which generates the images into the blog post.  
The only way I thought of is if somehow the blog post file name could be parsed, and a folder with that name in \_data would exist, where the user would put their images.  
I don’t really know how to do this.  
Can someone help?

---

<div class="post-metadata">

**Author:** ![Parmjot-Singh](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/parmjot-singh/32/5299_2.png) [@Parmjot-Singh](https://talk.jekyllrb.com/u/Parmjot-Singh)\
**Post date:** [April 29, 2023, 6:30am UTC](https://talk.jekyllrb.com/t/use-different-data-files-in-include-based-off-page-user-is-on/8196/2 "2023-04-29T06:30:51Z")

</div>

My theme is at [https://github.com/Parmjot-Singh/jekyll-theme-dusk](https://github.com/Parmjot-Singh/jekyll-theme-dusk)

---

<div class="post-metadata">

**Author:** ![pcouy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/pcouy/32/5307_2.png) [@pcouy](https://talk.jekyllrb.com/u/pcouy)\
**Post date:** [April 30, 2023, 7:08am UTC](https://talk.jekyllrb.com/t/use-different-data-files-in-include-based-off-page-user-is-on/8196/3 "2023-04-30T07:08:04Z")

</div>

Les’s imagine you have `_data/galleries/cats.yaml` and `_data/galleries/dogs.yaml`.

You access the variables contained in these files by using the following liquid variables : `data.galleries['dogs']` and `data.galleries['cats']`

On the other hand, you can pass variables to your included partials in the following way :

```auto
{% include image-masonry.html gallery_id='dogs' %}

```

This will make the string `'dogs'` available inside your partial as `include.gallery_id`. This means the variable `data.galleries[include.gallery_id]` will contain the variables defined in your `_data/galleries/dogs.yaml` file.

By accessing your data files this way, changing the value of `gallery_id` from the `include` tag will change the data file that your `image-masonry.html` partial accesses.

---

<div class="post-metadata">

**Author:** ![Parmjot-Singh](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/parmjot-singh/32/5299_2.png) [@Parmjot-Singh](https://talk.jekyllrb.com/u/Parmjot-Singh)\
**Post date:** [May 8, 2023, 6:45am UTC](https://talk.jekyllrb.com/t/use-different-data-files-in-include-based-off-page-user-is-on/8196/4 "2023-05-08T06:45:36Z")

</div>

Sorry for the late reply.  
This is not the solution I was looking for, but in a way, I guess this creates a better implementation of what I was trying to do. Thank you very much.

Here is the code I ended up with, disregarding CSS.

```auto
<div id="image-gallery">
    <ul>
        {%- for item in site.data.galleries[include.gallery_id] -%}
            <li>
                <img src="/assets/images/gallery/{{ include.gallery_id }}/{{ item.image }}">
            </li>
        {%- endfor -%}
    </ul>
</div>

```
