Is there a way to use 'eleventyExcludeFromCollections' in the eleventyComputed property? #2211
-
I'm trying to use page data to decide whether a page is excluded from collections, something like this: ---js
{
layout: "layouts/base.html",
pagination: {
data: "pages",
alias: "pg",
size: 1,
addAllPagesToCollections: true,
},
eleventyComputed: {
title: (data) => data.pg.title,
eleventyExcludeFromCollections: (data) => {
return data.pg.permalink === '/excluded-page';
},
}
}
--- However this seems to always return 'true' even if it's not. Even if I just 'return false' then all pages are then excluded from collections. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I would have expected your solution to work, but I'm struggling to figure out locally why it isnt working for me. // .eleventy.js snippet
eleventyConfig.addCollection("pages", function (collectionApi) {
const pages = [...collectionApi.getFilteredByTag("pages")];
return pages.filter(p => {
return !p.data.eleventyExcludeFromCollections;
});
}); ---
# src/pagination.njk
pagination:
data: "pages"
alias: "pg"
size: 1
addAllPagesToCollections: true
permalink: "{{ pg.permalink }}"
tags:
- pages
---
<h1>title={{ title }}</h1>
<p>permalink={{ pg.permalink }}</p>
<p>eleventyExcludeFromCollections={{ eleventyExcludeFromCollections }}</p>
<p>draft={{ pg.draft }}</p>
<p>{{ page | inspect }}</p> // src/pagination.11tydata.js (because I find it easier than JSON front matter. 🤷 )
module.exports = () => {
return {
eleventyComputed: {
title(data) {
return data.pg.title;
},
eleventyExcludeFromCollections(data) {
if (data.pg.draft || data.page.url === "/excluded-page/") {
return true;
}
return false;
},
},
};
}; src/_data/pages.json[
{"title": "Page 1", "permalink": "/page-number-one/" },
{"title": "Page two", "permalink": "/nested/page/two/", "draft": true},
{"title": "secret page", "permalink": "/excluded-page/"}
] ---
# src/collection-test.njk
---
{% for p in collections.pages %}
<p>{{ p.data.title }} {{ p.url }} {{ p.data.draft | inspect }}</p>
{% endfor %} OUTPUT<p>Page 1 /page-number-one/ undefined</p> Note that "Page 2" isn't listed (because we set custom |
Beta Was this translation helpful? Give feedback.
I would have expected your solution to work, but I'm struggling to figure out locally why it isnt working for me.
My quick workaround was to create a custom collection based on a tag and just re-filter the collection based on
eleventyExcludeFromCollections
frontmatter.