-
|
I’m trying to simplify some Eleventy setup by handling collections of entries that have a few tags in common. Something like (pseudo-code) One tag is trivial ( I tried several approaches, including using Am I missing something obvious, or is someone aware of a solution to this or a similar problem? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
|
Normally I'd just suggest that const tagMap = {
_carrots: ["vegetable", "carrot"],
_tomatoes: ["tomato"],
_vegetables: ["carrot", "tomato"], // returns 0 records since no pages have both "carrot" AND "tomato" as tags.
};
for (const [name, tags] of Object.entries(tagMap)) {
eleventyConfig.addCollection(name, collectionApi => collectionApi.getFilteredByTags(...tags));
}Or, another option if you want to dynamically query collections is maybe a hack like this (which might be a terrible idea, but seems to work): eleventyConfig.addCollection("dynamic", collectionApi => collectionApi);And then you can use it (in Nunjucks, LiquidJS is a bit trickier) like this: {% for p in collections.dynamic.getFilteredByTags('vegetable', 'carrot') %}
<p>{{ p.data.title }} -- {{ p.url }} -- {{ p.data.tags | dump | safe }}</p>
{% endfor %} |
Beta Was this translation helpful? Give feedback.
-
|
@j9t did you try using a filter? {% for item in collections.all | withTags('tag1', 'tag2', 'tag3') %} |
Beta Was this translation helpful? Give feedback.
Normally I'd just suggest that
getFilteredByTagsAPI and maybe a loop, and maybe something like this:Or, another option if you want to dynamically query collections is maybe a hack like this (which might be a terrible idea, but seems to work):
And then you c…