Dynamically accessing _data subfolders [SOLVED]

I’m building an art website, so most pages have some sort of image gallery. I’d like to have templates for the galleries that I can pass YAML files through, in order to generate a different gallery of images for each page. I’ve managed to get this working for files in the _data folder directly, but I’m stumped on how to pass a YAML file in a _data subfolder.

My project setup looks like this:

site/
  _data/
    projects/
      projectfoo.yml
      projectbar.yml
    artwork.yml
    character-design.yml
  _includes/
    gallery-cards.html
    project-cards.html
  projects
    projectfoo.md
    projectbar.md
artwork.md
character-design.md

Here’s the code from artwork.md:

# Major Projects
{% assign source = "artwork" %}
{% include project-cards.html %}

and the code from project-cards.html, which works:

<div class="card-container">
  {% for item in site.data[source] %}
    <a href="/projects/{{ item.link }}">
      <div class="card">
        <image src="{{ item.image }}" alt="{{ item.name }}"></image>
        <div class="card-title">
          {{ item.name }}
        </div>
      </div>
    </a>
  {% endfor %}
</div>

Meanwhile, the code from projectfoo.md:

{% assign source = "site.data.projects.projectfoo" %}
{% include gallery-cards.html %}

and the code from gallery-cards.html, which does not work:

<div class="card-container">
  {% for item in [source] %}
    <a href="{{ item.link }}">
      <div class="card">
        <image src="{{ item.image }}" alt="{{ item.name }}"></image>
      </div>
    </a>
  {% endfor %}
</div>

I’ve tried a few different variations of wording the source, including:

source = "projectfoo"
...
{% for item in site.data.projects[source] %}

and

source = "projects/projectfoo"
...
{% for item in site.data[source] %}

I can’t figure out how to do this for the life of me. Is it even possible, or will I have to settle for having all my YAML files in the main _data directory?

have you tried site.data.projects[source]? with source="projectfoo"?

not sure about it but maybe.

I would ditch the assign part and hard code it until it works, then work backwards and do the assign part.

the brackets - like [source] are a way to access a part of a data file - the part in brackets is usually sort of the index/item(?) - not sure I am explaining that right but your example of for item in [source] is not on track - the brackets would only mean something in the context of the prior part of the string/data file path(?). Course you are using a variable which is one extra layer of complexity. Normally this: site.data[source] would be site.data.artwork so maybe the brackets are cause you assigned artwork to a variable? not sure. Normally the brackets would be used to specify a data set in the file, like site.data.artwork[landscapes].

if you do this does it work?

gallery-cards.html
<div class="card-container">
  {% for item in site.data.projects.projectfoo %}
    <a href="{{ item.link }}">
      <div class="card">
        <image src="{{ item.image }}" alt="{{ item.name }}"></image>
      </div>
    </a>
  {% endfor %}
</div>

oh, weird - turns out that worked! I thought I already tried that, but I guess I must have had a typo in there somewhere, or maybe just tried something that was similar.

But for the record, yes, site.data.projects.projectfoo works as a hardcoded example! Thank you so much! And thank you for the explanation of the brackets, that makes a lot of sense.