-
Using Liquid’s I’m trying to use Nunjucks’ Then I attempted to write a new filter, but my JavaScript is very very limited. This is what I have:
Then, you can hard-code a custom, filtered collection within |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 12 replies
-
I haven't tried using Nunjucks' // Useful for trying to compare nested properties...
const _get = require("lodash.get");
module.exports = (eleventyConfig) => {
eleventyConfig.addNunjucksFilter("where", (arr, key, expected) => {
if (expected === undefined) {
return arr.filter(item => !!_get(item, key));
}
return arr.filter(item => _get(item, key) === expected);
});
// ...
} {% for post in collections.posts | where("data.draft", false) %}
<p data-draft="{{ post.data.draft }}">{{ post.data.title }}</p>
{% else %}
<p>No posts found</p>
{% endfor %} Not sure that it will work exactly like LiquidJS {
"draft": false,
"tags": ["posts"]
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I've got a where filter in on one of my projects:
Both of these also accept an array for the compare value. So you could do |
Beta Was this translation helpful? Give feedback.
-
Ah sorry - I store my filters in a separate file to my eleventy stuff. Wrapped in eleventy syntax:
|
Beta Was this translation helpful? Give feedback.
I haven't tried using Nunjucks'
rejectattr
filter yet, but this might get you somewhat closer:Not sure that it will work exactly like LiquidJS
where
…