Creating collection based on subset of global data file #1916
-
Hi, I've been searching everywhere for a solution to my specific issue but have yet to find something that works (or that I can make work). A quick caveat: I'm mainly a designer/html/css person so I'm brand new to 11ty and js, so please have mercy on me :) The Background
For the most part 11ty and Nunjucks let me do everything I need to do. But the thing that's tripping me up is the 'featured: true' property. Rather than using nested for statements with Nunjucks, I'd like to create a custom collection (e.g. collections.featProducts) from this data that gives me the featured products. The most central reason why I'd like to create a collection is so I have an easy way to limit the output (e.g. for product in collections.featProducts | limit(3) ). I haven't been able to work this out in Nunjucks. If there's way to do that then I'd be happy to hear that as well :) Back to the original question: I have found several resources that get me part of the way there, but unfortunately I haven't been able to work out the proper way to filter the data down to the correct subset of objects. The Collection So far the best starting point I have is a combination of what's shown on 11ty.rocks (https://11ty.rocks/eleventyjs/collections/) and the 11ty collections documentation. That basically looks like...
It's that "// ..." void where I've tried all sorts of approaches to no avail. I've looked all over StackOverflow and various google results, but everything I've come across for filtering the array to return the correct objects doesn't seem to work within the .eleventy.js config file. Using dot notation always seems to fire off an error along the lines of 'can't get length of undefined'. As I'm sure is clear, I don't really know what I'm doing on this front but I'm certain someone here knows exactly what to do. Thank you in advance to anyone who reads this! And apologies if I managed to miss this exact answer somewhere after googling about 400 permutations of "eleventy create collection from custom data". |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Do you have a public repo I can try cloning? Also, what is a featured product? Is it specific to each category? Or are you expecting to merge all |
Beta Was this translation helpful? Give feedback.
-
One quick addition in case it's of use to anyone in the future: I took suggestion 2 from @pdehaan and modified it based on the 11ty docs and 11ty.rocks resources to create a featuredProducts collection. It's fundamentally the same as that 2nd option but is structured slightly differently and doesn't need the require step:
Thanks again to @pdehaan for the help! |
Beta Was this translation helpful? Give feedback.
Do you have a public repo I can try cloning?
Also, what is a featured product? Is it specific to each category? Or are you expecting to merge all
category
'sitems
into a single array and then filter allfeatured:true
products?