-
-
Notifications
You must be signed in to change notification settings - Fork 491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't iterate over a global data subfolder #304
Comments
You should be able to iterate over them, but it looks like you're trying to access them in your template as if the files lived at A file at Is the data iterable as arrays? Not sure which template language you're using, but you may be trying to iterate over objects when the language expects an array. |
Thanks a lot for answering. For some sections (collection pages) of my site, I use JS files (that could be generated by a back-end DB if necessary). Each section has its JS files and I know how to access each one when I know the section item (JS file) requested. But if I want to iterate over all files (i.e. exported Object/Array modules) within a section, the Nunjucks for loop gives nothing.
are accessed as
and I can iterate on But I cant iterate on
However, |
Right. |
@kleinfreund: Your answer confirms what I found. My workaround is a small JS module I've written to iterate over the files in the data folders I want to use: a bit more complicated but it works. |
Yup, Your workaround is the right choice; build up the structure yourself, then iterate over your own structure. In cases like these, I've found myself storing the data outside of |
I think this is maybe related to overrhiding something in an md. If I use data: [{ etc etc }] the data is populated if I do data: subfolder.data with the same content of the inline array of objects I don't get any data. How the js should be created in order to make the sufolder object accessible? |
You'd just need to create a // _data/users.js
const user1 = { name: 'Jane', id: 1 }
const user2 = { name: 'John', id: 2 }
module.exports = () => {
return [
user1,
user2
]
} {% for user in users %}
<p>{{ user.name }} has an ID of {{ user.id }}</p>
{% endfor %} If you really need each user as its own data file, too: // _data/users/user1.json
{
"id": 1,
"name": "Jane"
} // _data/users/user2.json
{
"id": 2,
"name": "John"
} // _data/users.js
const user1 = require('./users/user1.json')
const user2 = require('./users/user2.json')
module.exports = () => {
return [
user1,
user2
]
} Bottom line is: if you need |
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. |
@jevets: Woo! The Array solution is pretty expansive because I can have many data files to declare and the
That module can reside anywhere in the source folder. Using JS data files shines, compared to using JSON files or Yaml/Toml etc.. Not only your code is lighter (every JS programmer hates the JSON property quotes, without optional comments!) but you can introduce a bunch of JS processing in your data files, using the full power of a programming language. JSON data files are pure declaration files, JS data files are pure programming files. |
@andreapernici: AFAIK you can't use JS data inside your front matter (even if you use JS front matter). You can just invoque JS library functions I think. IMO, it's a better practice to use front-matter only to declare page constants and use JS data files to process the data as you see fit. |
Yes I noitced that putting those variables in the NJK works, but in my use case it could be useful to overrhide those value based on a JSON file downloaded from external APIs. Obviously creating the js object file is not as straight forward as curling the API response. I'll end up probably create a sort of dynamic templates that uses a var as the folder selector id is not possible to dynamically overrhide values. |
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 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:
Does that help everyone? |
(More info at https://mozilla.github.io/nunjucks/templating.html#for) |
Could've sworn I tested that Was actually thinking about suggesting this as a feature, but it's already available! Thanks for the clarification. The @octoxalis @kleinfreund my statement above that |
No worries @jevets—just a simple mistake 👍 I appreciate the help you’ve been providing on the tracker!! |
It's perfect. Sometimes the most evident things are out of scope! |
Thanks all! Closing |
The
I can But I can't figure out how to use the data in a template like so: Any pointers? |
@plainspace Assuming a data file of ./_data/team/bob.json, I believe you'd use something like the following: <p>Hello, my name is {{ team.bob.name }} and you can tweet me at https://twitter.com/{{ team.bob.twitter }}.</p> <p>Hello, my name is {{ team.bob.name }} and you can tweet me at https://twitter.com/{{ team.bob.twitter }}.</p>
{# Output:
<p>Hello, my name is Bob and you can tweet me at https://twitter.com/bobstwitter.</p>
#}
{# loop over the _data/team/bob.json object... #}
{%- for key, value in team.bob %}
KEY={{ key }}, VALUE={{ value }}<br/>
{%- endfor %}
{# Output:
KEY=name, VALUE=Bob<br/>
KEY=github, VALUE=bobsgithub<br/>
KEY=twitter, VALUE=bobstwitter<br/>
KEY=linkedin, VALUE=https://www.linkedin.com/in/bob/<br/>
KEY=blog, VALUE=https://medium.com/@bob<br/>
#}
{# debug the _data/team/members.json array... #}
<p>Members: {{ team.members | dump(2) | safe }}</p>
{# Output:
<p>Members: [
"bob",
"david"
]</p>
#}
{# loop over the array of names in _data/team/members.json and
fetch their respective _data/team/{{ name }}.json file... #}
<ul>
{%- for member_name in team.members %}
{%- set member = team[member_name] %}
<li>{{ member.name }} — @{{ member.twitter }}</li>
{%- endfor %}
</ul>
{# Output:
<ul>
<li>Bob — @bobstwitter</li>
<li>David — @davidstwitter</li>
</ul>
#} |
Thanks! In the last example, what does the |
Also, in the first example the output is:
|
🐥I figured it out. Thanks! |
[
"bob",
"david"
] |
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 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
|
@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 [
{"name": "bob", "weight": 20},
{"name": "david", "weight": 10}
] Then our nunjucks template can sort by weight: <ul>
{%- for member in team.members | sort(false, false, "weight") %}
<!-- {{ member | dump | safe }} -->
{%- set weight = member.weight %}
{%- set member = team[member.name] %}
<li>{{ member.name }} — @{{ member.twitter }} — {{ weight }}</li>
{%- endfor %}
</ul> OUTPUT: <ul>
<!-- {"name":"david","weight":10} -->
<li>David — @davidstwitter — 10</li>
<!-- {"name":"bob","weight":20} -->
<li>Bob — @bobstwitter — 20</li>
</ul> 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. |
Hey, can you tell us how you figured this out? Did you go with the solution of having a members.json file or did you find a way to pull data from a specific member despite the subdirectory? |
My global data are contained in subfolders of the main data folder.
For instance:
data/subdata
.My Nunjucks for loop is:
However I never get anything: no looping!
But if I put:
subdata.entry.property
orsubdata['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?
The text was updated successfully, but these errors were encountered: