-
|
Is there any way to get a single item from a collection (for example by selecting it by it's slug), or do I have to loop through the entire collection and select it with an if-statement? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
I'd probably just create a custom filter that takes a collection and whatever property you want to filter by, and then have the filter return an object/null whether the item was found. Maybe something like this: eleventyConfig.addFilter("find", function find(collection = [], slug = "") {
// If you want more advanced, dynamic filtering, you might need https://lodash.com/docs/4.17.15#get
// for fetching [deeply] nested properties.
return collection.find(post => post.url === slug);
});---
# /src/pages/2.njk
title: Project 2
---
<h1>{{ title }}</h1>
{%- set page3 = collections.projects | find("/pages/3/") -%}
<h2>{{ page3.data.title }}</h2> |
Beta Was this translation helpful? Give feedback.
-
|
I have solved it now by just creating a custom collection that is just an object with all the data I need. then I can access it anywhere on my site via |
Beta Was this translation helpful? Give feedback.
I have solved it now by just creating a custom collection that is just an object with all the data I need.
then I can access it anywhere on my site via
{{ collections.dataObject[id].metadata }}