eleventy: Can't iterate over a global data subfolder
My global data are contained in subfolders of the main data folder.
For instance: data/subdata
.
My Nunjucks for loop is:
{% for sub in subdata %}
.....
{% endfor %}
However I never get anything: no looping!
But if I put:
subdata.entry.property
or subdata['entry'].property
I get that property. I mean, if I know the name of the data subfolder, I can retrieve anything declared in that data file.
Does it mean that I can’t iterate over the data subfolders?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 26 (8 by maintainers)
Whoa, this thread kinda went off the rails and there is a lot of misinformation in here—sorry, everyone. Wish I would’ve stepped in sooner.
The original problem @octoxalis had was a simple misunderstanding of how Nunjucks loops work.
Consider the following project structure:
Here’s what
{{ subdata | dump }}
looks like:It’s true that this outputs nothing:
However that’s only because you’re looping over an object literal, not an array. Absolutely you can iterate over subdata!
This is what you want:
Output:
Does that help everyone?
The
key, value
piece is tripping me up. I have multiple files in_data/team
with a name likebob.json
and structured like so:I can
{{ team | dump }}
and see all of the json output.But I can’t figure out how to use the data in a template like so:
{{ name }}
,{{ blog }}
, etc.Any pointers?
@plainspace You could move the weights into the members.json file. I can’t see how you could create a custom collection from files in the
_data/
directory.Then our nunjucks template can sort by weight:
OUTPUT:
You could also move all the members into a single data file to make it easier to sort. But it really depends on how you’re using the data.
Thanks! Not sure if this is the right place to ask but let’s see:
I’d like to sort the data now. Some of the
_data/team/{{ name }}.json
files have aweight
attribute in them.I’m expecting that the following would move the members with a weight to the top of the list and order them ascending and then order those without a weight alphabetically. I’m assuming that isn’t working because the order in
members.json
is defining the order.{%- for member_name in team.members|sort(attribute='weight') %}
No worries @jevets—just a simple mistake 👍 I appreciate the help you’ve been providing on the tracker!!
I think I’m missing something here but for example considering the json file to be already an array of object.
_data/2013_feedbacks.js
then applying the following in an .md file
I expect feedback to be populated with the content of _data/2013/feedbacks.json but I don’t get any value.