Collections within collections - are they possible? #1735
-
I'm struggling to figure something out and I'm here asking for help on whether it's possible, or whether I'm trying to be too adventurous. I followed this tutorial on creating pages from data: https://www.11ty.dev/docs/pages-from-data/ My data is in /_data/venues.json and it happily generates venue1/index.html, venue2.html, venue3/index.html, and so on. Everything is great so far. My question is... How/Can I then output data from another collection within those generated files? I have /_data/venue/venue1.json, /_data/venue/venue2.json, /_data/venue/venue3.json and I'm (trying) using nunjucks. In my venue-page.njk which generates the html pages, I have this:
and it doesn't work. I've also tried:
and unfortunately this doesn't work either. I'm hoping this will be a simple answer for someone who knows a lot more about 11ty / nunjucks / json than I do. Is what I'm trying to do even possible? And if so, how might I go about doing it? Thank you so much for any help / insight you can offer, even if it's just 'ahahaha no this will not work.' |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I definitely think it's possible, but might be a bit tricky based on how your folders and variables are named. Here's my _data/venues.json file: [
{ "name": "Venue 1", "key": "venue1" },
{ "name": "Venue 2", "key": "venue2" }
] And my _data/venue/venue1.json (and venue2.json) files look roughly like this: {
"name": "Venue 1",
"address": "100 Main St, City, State, Country"
} This is where is starts to get a bit tricky... ---
title: Venues
pagination:
data: venues
size: 1
alias: _venue
permalink: "/{{ _venue.name | slug }}/"
---
{# `venue` here refers to /_data/venue/* directory. #}
{% set $venue = venue[_venue.key] %}
<p>name={{ $venue.name }}</p>
<p>address={{ $venue.address }}</p>
<p>venues={{ venues | dump }}</p> First we're looping over the I'm happy to push my sample repo up to GitHub if you want to take a look at my workaround, or I can help troubleshoot your code if you have a public repo. |
Beta Was this translation helpful? Give feedback.
I definitely think it's possible, but might be a bit tricky based on how your folders and variables are named.
Here's my _data/venues.json file:
And my _data/venue/venue1.json (and venue2.json) files look roughly like this:
This is where is starts to get a bit tricky...
Here's my venue-page.njk template: