Two posts, each one in a different collection, but with the same tag, are both outputted in both collections
rocc-o opened this issue · comments
I have two separate collections: /sounds and /sights
and in each directory the tags output like this:
/sights/tags/(all tags for sights);
/sounds/tags/(all tags for sounds).
But when I have two posts, one in /sounds and one in /sights, both with the same tag, the posts are repeated in both directories.
Posts:
/sights/video-one.njk (tag: jazz-funk)
/sounds/audio-one.njk (tag: jazz-funk)
Outputs:
/sights/tags/jazz-funk/
- audio-one.html
- video-one.html
(but it should be only video-one.html)
/sounds/tags/jazz-funk/
- audio-one.njk
- video-one.njk
(but it should be only audio-one.html)
Here's my repo which reproduce the problem: https://github.com/rocc-o/raw-test-git
The two posts, each one in a different collection but with the same tag (jazz-funk), are:
/sights/video-one.njk
/sounds/audio-one.njk
I get the following error when trying to build your site:
> You’re trying to use a layout that does not exist: _below-layout.njk (undefined)
Also, you should add a package.json with your dependencies to the repo. Makes it easier to clone and get started.
Sorry, the repo is a stripped version of the site in production and sometimes I lose the pieces.
I have now updated the layout and added the package.json.
https://github.com/rocc-o/raw-test-git
OK, I think I know what the problem is, but not sure if I have a possible solution yet…
https://github.com/rocc-o/raw-test-git/blob/main/sights_tags.njk#L16
{% set postListItems = collections[tag] %}
Since the pagination is looping over the audio/video specific tags and then pointing to a collection, the collection will mix audio+video since they both use the same tag.
My approach would probably be adding some directory specific data files like sights/sights.11tydata.js and sounds/sounds.11tydata.js and set some category: "sights"
style data and then using that to filter. Possibly somewhere in https://github.com/rocc-o/raw-test-git/blob/main/_includes/partials/components/post-list.njk where you're displaying the posts.
If you're lucky, something like this might work.
{% if item.data.category === category %}...{% endif %}
I see. Please leave me a couple of days to try this solution.
One thing though: I see that Specific Data Files is new in v0.2.14, and I'm running version 0.12.1.
Do I have to update Eleventy? If so, how can I do that? Do I have to re-install everything from scratch?
How about the code in use? Will be new v0.2.14 backward compatible?
Thanks.
I've tried this solution - and updated the repo as well: https://github.com/rocc-o/raw-test-git
But the problem persists. Could it be because I'm running Eleventy version 0.12.1?
-
0.12.1 is bigger/newer than 0.2.14, so that's fine. We're running the latest stable release.
-
We were close! We just need a few tweaks:
- the *.11tydata.js files need to
module.exports
the value. - need to explicitly set the
category
tosights
in thesights_tags.njk
file since it lives outside thesights/
folder and won't pick up that data file (and same w/ sounds_tags.njk).
- the *.11tydata.js files need to
diff --git a/.eleventy.js b/.eleventy.js
index 18be627..d647e6b 100644
--- a/.eleventy.js
+++ b/.eleventy.js
@@ -1,6 +1,7 @@
module.exports = function (eleventyConfig) {
+ eleventyConfig.setDataDeepMerge(true);
const now = Date.now();
const livePosts = (post) => post.date <= now;
diff --git a/sights/sights.11tydata.js b/sights/sights.11tydata.js
index 6814c65..232c7f8 100644
--- a/sights/sights.11tydata.js
+++ b/sights/sights.11tydata.js
@@ -1 +1,3 @@
-category: "sights"
+module.exports = {
+ category: "sights"
+};
diff --git a/sights_tags.njk b/sights_tags.njk
index 81eb5b8..7b25d24 100644
--- a/sights_tags.njk
+++ b/sights_tags.njk
@@ -9,6 +9,7 @@ pagination:
- post
addAllPagesToCollections: true
permalink: "/sights/tags/{{ tag | slug }}/"
+category: sights
---
{% extends 'layouts/base.njk' %}
diff --git a/sounds/sounds.11tydata.js b/sounds/sounds.11tydata.js
index 689557c..917cabe 100644
--- a/sounds/sounds.11tydata.js
+++ b/sounds/sounds.11tydata.js
@@ -1 +1,3 @@
-category: "sounds"
+module.exports = {
+ category: "sounds"
+};
diff --git a/sounds_tags.njk b/sounds_tags.njk
index 3f6f87c..9faa793 100644
--- a/sounds_tags.njk
+++ b/sounds_tags.njk
@@ -9,6 +9,7 @@ pagination:
- post
addAllPagesToCollections: true
permalink: "/sounds/tags/{{ tag | slug }}/"
+category: sounds
---
{% extends 'layouts/base.njk' %}
Great! It works!
One problem though: last articles on index homepage are gone
<h2>Latest articles in Sights</h2>
{% set postListItems = collections.sights | head(4) %}
{% include "partials/components/post-list.njk" %}
<h2>Latest articles in Sounds</h2>
{% set postListItems = collections.sounds | head(4) %}
{% include "partials/components/post-list.njk" %}
Ah...
You might need to set the category
on that page too, or possibly add more logic to the post-list.njk include file to account for empty/null categories.
See if this works:
<h2>Latest articles in Sights</h2>
{% set postListItems = collections.sights | head(4) %}
+{% set category = "sights" %}
{% include "partials/components/post-list.njk" %}
<h2>Latest articles in Sounds</h2>
{% set postListItems = collections.sounds | head(4) %}
+{% set category = "sounds" %}
{% include "partials/components/post-list.njk" %}
Yes! Like a charm! Thank you so much Peter!