Using one JSON file as a data source for two templates #3872
-
In Eleventy, I've got a global data source called meat.json. There's a template file called meat-category.njk, and a layout file called supermarket-category.njk. I understand that if I want to call upon data in meat.json, then I have to reference However, I really need to avoid hard-coding references to 'meat' in supermarket-category.njk, because next week I might want to swap out meat.json for another data source called freshfruit.json or confectionary.json. I'm having trouble passing the data from the template to the layout; nothing comes through. Here's meat-category.njk –
And here's the start of supermarket-category.njk –
Eleventy outputs to the browser –
The dump filter stringifies [object Object]; and using category.categoryTitle returns nothing. Any help is gratefully accepted [prayer hands]
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I'm not 100% sure what you're actually trying to achive here, can you give a little more detail. Maybe the contents of your data files? Or an illustration of what is in them. I might be wrong, but I don't think you can set a key in front-matter to be the contents of a data file. That is why you're struggline with : Meat Products
category dump: [object Object] Reading between the lines, you want to avoid having to rewrite your entire template each time you want to move beween meat and veg. Assuming the two json data files share the same strucutre you could do something like this (in Nunjcks): {
"description": "A collection of meats, each with a unique name and price.",
"products": [
{
"name": "Beef",
"price": 5.2
},
{
"name": "Chicken",
"price": 4.5
},
{
"name": "Pork",
"price": 2.0
}]
}
{
"description": "A collection of vegetables, each with a unique name and price.",
"products": [
{
"name": "Carrot",
"price": 1.2
},
{
"name": "Broccoli",
"price": 1.5
},
{
"name": "Spinach",
"price": 1.0
}]
} {% set currentProduce = meat %}
<h2>{{ currentProduce. description}}</h2>
<ul>
{% for product in currentProduce.products %}
<li>
<h2>{{ product.name }}</h2>
<p>Price: {{ product.price }}</p>
</li>
{% endfor %}
</ul> And when you want to change it to veg youjust need to update |
Beta Was this translation helpful? Give feedback.
-
You could also pull the different food products into a third data file
which is the aggregate of all products. I do something like this for my
personal site to merge all blog posts, video recordings, and podcasts into
a "content" data set.
…On Sun, Jun 22, 2025, 8:52 PM dwkns ***@***.***> wrote:
I'm not 100% sure what you're actually trying to achive here, can you give
a little more detail. Maybe the contents of your data files? Or an
illustration of what is in them.
I might be wrong, but I don't think you can set a key in front-matter to
be the contents of a data file. That is why you're struggline with :
Meat Products
category dump: [object Object]
Reading between the lines, you want to avoid having to rewrite your entire
template each time you want to move beween meat and veg. Assuming the two
json data files share the same strucutre you could do something like this
(in Nunjcks):
meat.json
{
"description": "A collection of meats, each with a unique name and price.",
"products": [
{
"name": "Beef",
"price": 5.2
},
{
"name": "Chicken",
"price": 4.5
},
{
"name": "Pork",
"price": 2.0
}]
}
veg.json
{
"description": "A collection of vegetables, each with a unique name and price.",
"products": [
{
"name": "Carrot",
"price": 1.2
},
{
"name": "Broccoli",
"price": 1.5
},
{
"name": "Spinach",
"price": 1.0
}]
}
{% set currentProduce = meat %}<h2>{{ currentProduce. description}}</h2>
<ul>
{% for product in currentProduce.products %}
<li>
<h2>{{ product.name }}</h2>
<p>Price: {{ product.price }}</p>
</li>
{% endfor %}
</ul>
And when you want to change it to veg youjust need to update {% set
currentProduce = veg %} and the rest of it will take care of itself. If
you didn't mean this, give us a little more detail (or better still share a
repo) and we'll try and help.
—
Reply to this email directly, view it on GitHub
<#3872 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABEOLMLVVGU7GT7D4CZW5J33E5FVXAVCNFSM6AAAAAB7W5GB5WVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTGNJUGU2TOMY>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
I'm not 100% sure what you're actually trying to achive here, can you give a little more detail. Maybe the contents of your data files? Or an illustration of what is in them.
I might be wrong, but I don't think you can set a key in front-matter to be the contents of a data file. That is why you're struggline with :
Reading between the lines, you want to avoid having to rewrite your entire template each time you want to move beween meat and veg. Assuming the two json data files share the same strucutre you could do something like this (in Nunjcks):
meat.json